我有一个python类,它有多个方法.我已经通过@staticmethod实例定义了我的方法,我想从我的main函数(main_function)中调用我的类的其他方法.我想我需要self从我的main函数调用我的其他函数的参数,并且我想main_function在创建我的类的实例时将此参数传递给我.
class myclass:
@staticmethod
def function1(param1)
print "function1"
@staticmethod
def main_function(self, param1)
function1(param1)
my_object = myclass()
my_object.main_function(param1)
Run Code Online (Sandbox Code Playgroud)
我收到了这个错误:
TypeError: main_function() takes exactly 2 arguments (1 given)
Run Code Online (Sandbox Code Playgroud)
问题是我self在创建实例时没有参数.我尝试@staticmethod从我的方法定义中删除关键字并删除所有self参数,但这不起作用.
我有一个名为settings的python类,其中包含一个__init__设置如下值的方法:
class settings:
global appkey
global appversion
def __init__(self):
appkey = 1
appversion = 1
applicationname = "app1"
applicationfile = "app.txt"
Run Code Online (Sandbox Code Playgroud)
在另一个python文件(主脚本)中,我通过以下代码定义了我的设置类的实例:
import settings
from settings import settings
set = settings()
print set.appversion
print set.appkey
print set.applicationfile
Run Code Online (Sandbox Code Playgroud)
但是当我运行我的主要python脚本时,我收到了这个错误:
AttributeError: settings instance has no attribute 'appversion'
Run Code Online (Sandbox Code Playgroud)
我希望当我在这里定义一个类的实例,设置类时,它的init函数将被触发,我将拥有其属性的值.