Python 从 class 获取值
Python getting values from class
我正在尝试制作一个 运行 几个文件以使其组织起来的应用程序,但是我有点潜伏在 python 中并试图从 settings.py
中导入值 [= =17=].
根据我尝试获取对象值的方式,我得到的错误类型如下所示:
self.code = sets.run().self.code
AttributeError: 'NoneType' object has no attribute 'self'
或
self.code = sets.code
AttributeError: 'settings' object has no attribute 'code'
整个思路就是让get self。另一个文件中的值不使用 return 而是简单地指定元素。
简单示例代码:
主文件:
import settings
def run():
sets = settings.settings()
code = sets.code
run()
设置文件:
class settings():
def run(self):
self.code = somevalue
所以基本上这是我想要做的事情的最小扩展,我可以 return 运行 中的 self.code 但这不是我想要的,因为我有很多 settings_values 和 id 喜欢在不同的文件中访问它们。
帮助非常感谢。
Edit:
So how should i approach this if i need to import settings
in main
but only run() settings whenever i start running the main_run() and than just get the variables?
I dont want to complicate it with saving to file and than reading a list() i need to just access them and only run
basicaly: my settings gets a long list from pickle and than makes some operations that return the code combination and lots of other self.settings that need to be initiated when the program(main) starts and than for the whole While True inside run() the values are fixed unless i start refresh_settings() and change the settings than the data is recompiled and the program returns to while true
第一个错误:run
没有 return 语句,因此默认为 returns None
。然后你尝试做 None.self.code
显然得到一个 AttributeError
.
第二个错误:settings
实例的属性 code
似乎是在第一次执行 settings.run
时设置的。您可能试图在 运行 settings.run
.
之前访问 sets.code
你的例子不是完全独立的,所以我不能告诉你比这个一般的指针更多的东西。
您需要 __init__
函数,您的 class 应该如下所示:
class settings():
def __init__(self,code):
self.code = code
现在你可以像这样创建一个变量
sets = settings(“something”)
并以这种方式访问变量中的code
:
code = sets.code
或者如果您真的想执行 run()
位,那么您的代码将是:
class settings():
def __init__(self,code):
self.code = code
def run(self):
return self.code
我正在尝试制作一个 运行 几个文件以使其组织起来的应用程序,但是我有点潜伏在 python 中并试图从 settings.py
中导入值 [= =17=].
根据我尝试获取对象值的方式,我得到的错误类型如下所示:
self.code = sets.run().self.code
AttributeError: 'NoneType' object has no attribute 'self'
或
self.code = sets.code
AttributeError: 'settings' object has no attribute 'code'
整个思路就是让get self。另一个文件中的值不使用 return 而是简单地指定元素。
简单示例代码:
主文件:
import settings
def run():
sets = settings.settings()
code = sets.code
run()
设置文件:
class settings():
def run(self):
self.code = somevalue
所以基本上这是我想要做的事情的最小扩展,我可以 return 运行 中的 self.code 但这不是我想要的,因为我有很多 settings_values 和 id 喜欢在不同的文件中访问它们。 帮助非常感谢。
Edit: So how should i approach this if i need to
import settings
inmain
but only run() settings whenever i start running the main_run() and than just get the variables? I dont want to complicate it with saving to file and than reading a list() i need to just access them and only runbasicaly: my settings gets a long list from pickle and than makes some operations that return the code combination and lots of other self.settings that need to be initiated when the program(main) starts and than for the whole While True inside run() the values are fixed unless i start refresh_settings() and change the settings than the data is recompiled and the program returns to while true
第一个错误:run
没有 return 语句,因此默认为 returns None
。然后你尝试做 None.self.code
显然得到一个 AttributeError
.
第二个错误:settings
实例的属性 code
似乎是在第一次执行 settings.run
时设置的。您可能试图在 运行 settings.run
.
sets.code
你的例子不是完全独立的,所以我不能告诉你比这个一般的指针更多的东西。
您需要 __init__
函数,您的 class 应该如下所示:
class settings():
def __init__(self,code):
self.code = code
现在你可以像这样创建一个变量
sets = settings(“something”)
并以这种方式访问变量中的code
:
code = sets.code
或者如果您真的想执行 run()
位,那么您的代码将是:
class settings():
def __init__(self,code):
self.code = code
def run(self):
return self.code