Reputation: 9672
I am trying to construct an ETL machine for a test.
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
for get_distinct_emails
I want to call TestETLMachine.get_email_data()
and have it know I'm referring to this class. This object is a static machine, meaning it does the same thing always, and making instances of it is pointless and seems bad form. When I try to call get_email_data
now that I pass cls
I can't anymore:
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)
How do I call these class methods and use the other class methods in my next class methods? Salamat
Upvotes: 0
Views: 73
Reputation: 601679
You are looking for classmethod
, not staticmethod
. If you decorate a method with @classmethod
, it will implicitly receive the class as first parameter.
See also the related question Meaning of @classmethod and @staticmethod for beginner?
Upvotes: 7