0

In the example below, How do I call function 1 in function 2. I have tried it just like in the example, but does not recognise para in function 2 and I have also tried replacing it with self.para which gets a undefined error..what is the right way to call function 1? I can't add another argument in function2(self).

class Example:
    #def...

    def function1(self, para):
        #bla bla
    
    def function2(self):
        xxx = self.function1(para)
        # how do I call a function such as this
3
  • What argument do you want to pass to para? self.function1(<that argument goes here>) Commented Sep 26, 2022 at 10:59
  • What example are you referring to? What error do you see? Commented Sep 26, 2022 at 11:01
  • Please, post minimal reproducible example Commented Sep 26, 2022 at 11:02

1 Answer 1

3
class Example:
    #def...

    def function1(self, para):
        #bla bla
    
    def function2(self):

        para = 12  # make sure the variable you pass to function1 is defined        

        xxx = self.function1(para)
        # how do I call a function such as this

You can only reference self.para if class Example has been instantiated and has an attribute called para.

If class Example has attributes that are static, you can reference those through an instance (self) or through the class.

Instantiating and referencing self:

class Example:
    def __init__(self):  # constructor method
        self.value = 12  # instance attribute

    def func1(self, parameter):
        # do stuff
        return ...

    def func2(self):
        x = func1(self.value)

or from static context:

class Example:

    value = 12  # class attribute

    def func1(self, parameter):
        # do stuff
        return ...

    def func2(self):
        x = func1(Example.value)
Sign up to request clarification or add additional context in comments.

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.