对于staticmethods,将self称为“this class”;python

2024-09-30 18:34:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试为测试构建一个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)

如何调用这些类方法并在下一个类方法中使用其他类方法?萨拉马特


Tags: 方法机器apijsonurldatagetobjects