0

I am trying to have Nested function inside a class. Here is my code

class big():
        def __init__(self):
        self.mas = "hello"
    
    def update(self):
        
        def output(self):
            print(self.mas)
        
        self.output()
        
thing = big()

thing.update()

However when it runs I get an error that output is not defined. How can i run the output function inside the update function?

1 Answer 1

3

Just call it as output(), without self. The way you've defined it, it basically is a local variable inside your update method, not an attribute of the class.

class big():
    def __init__(self):
        self.mas = "hello"

    def update(self):
        def output():
            print(self.mas)

        output()

thing = big()

thing.update()
Sign up to request clarification or add additional context in comments.

2 Comments

You should probably not have self as arg to output, but if you do, you should pass it with the call. As it stands, it raises TypeError: output() missing 1 required positional argument: 'self'
@ReblochonMasque My mistake, fixed it

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.