0

I want to display the value of a variable in world function.

def base():
    
    print("Hello World")
    
    def child():
        a = 10
        return a
    
def world():
    num = base.child() # error -------
    print(num)
    
world()```
4
  • 1
    What does base represent in the expressions base.child()? A scalar value, an object, a class, a method, or what? Commented May 11, 2021 at 15:21
  • base is a function Commented May 11, 2021 at 15:23
  • does a function have an attribute 'child'? Commented May 11, 2021 at 15:27
  • The base function have the child function. Commented May 11, 2021 at 15:31

1 Answer 1

1

I suggest calling child in base.

def base():
    
    print("Hello World")
    def child():
        a = 10
        return a
    return child()
def world():
    num = base()
    print(num)
    
world()
Sign up to request clarification or add additional context in comments.

2 Comments

Now it's working, but I want another way (Don't return the child function from base function)
I don't think there's any way to access the inner function from outside. That's kind of the point. The best you can hope for is to access the results of the inner function, exactly as @BuddyBob suggests. You might find this informative. Python Inner Functions: What Are They Good For?

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.