0

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.

3
  • 2
    if do_this_part is inside the class, you need to call it as self.do_this_part(response). Also you need to change its signature so that it takes self as the first argument, and response as the second. Commented Jun 22, 2020 at 19:49
  • Yes, adding to Robin's answer, please edit your function like this - def do_this_part(self, response): Commented Jun 22, 2020 at 19:52
  • Thanks @RobinZigmond and user 78910! Commented Jun 22, 2020 at 20:02

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.