我正在尝试为测试构建一个ETL机器。你知道吗
class TestETLMachine(object):
API_URL = 'https://9g9xhayrh5.execute-api.us-west-2.amazonaws.com/test/data'
@staticmethod
def get_email_data(cls):
headers = {'accept': 'application/json'}
r = requests.get(cls.API_URL, headers=headers)
email_objects_as_list_of_dicts = json.loads(r.content)['data']
return email_objects_as_list_of_dicts
@staticmethod
def get_distinct_emails(cls):
email_data = cls.get_email_data()
print email_data
对于get_distinct_emails
,我想调用TestETLMachine.get_email_data()
,让它知道我指的是这个类。这个对象是一个静态的机器,这意味着它总是做同样的事情,对它进行实例处理是毫无意义的,而且形式看起来很糟糕。当我试着打电话给get_email_data
现在我通过了cls
我不能再:
In [9]: TestETLMachine.get_email_data()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-9-cf48fc1a9c1d> in <module>()
----> 1 TestETLMachine.get_email_data()
TypeError: get_email_data() takes exactly 1 argument (0 given)
如何调用这些类方法并在下一个类方法中使用其他类方法?萨拉马特
你要找的是^{} ,而不是
staticmethod
。如果用@classmethod
修饰方法,它将隐式接收类作为第一个参数。你知道吗另见相关问题Meaning of @classmethod and @staticmethod for beginner?
相关问题 更多 >
编程相关推荐