Reputation: 494
I'm writing a class to get data from an API. I have functions that I want to perform on the class e.g.
self.do_this()
I also have functions that are supposed to do things within that function, e.g.
def do_this_part(response):
Dict = {}
for key, value in response.items():
Dict[key] = value
return Dict
I want to have a smaller function inside the class so that it always comes within the class. It will never be performed on the class but will be performed within multiple functions that will be performed in the class.
I am getting the following error:
NameError: name 'do_this_part' is not defined
So clearly I'm not defining the function in the right place. My apologies if this is a fairly basic question that has been answered before, I searched and couldn't find the answer.
Upvotes: 0
Views: 30
Reputation: 1251
It sounds to me like you want to use a staticmethod.
class C:
def do_this(self):
response = {}
self.do_this_part(response)
@staticmethod
def do_this_part(response):
Dict = {}:
for key, value in response.items():
Dict[key] = value
return Dict
Upvotes: 1
Reputation: 3130
It sounds like you're looking to do something like this:
class MyClass():
def do_this_part(self, response):
# Implement your logic here
print(f"Response parameter is {response}")
my_class = MyClass()
response = "yo"
my_class.do_this_part(response)
Upvotes: 0