8

I have a function inside inside another and a third function. How can I call my nested function inside of my third function? Is there any special libraries I can use? I am not allowed to edit a() or b(), only c().

def a():
    def b():
        print("hi")

def c():
    # code only here to call b() to print
5
  • 1
    Why did you nest it if you want to call it from outside? Commented Jun 10, 2018 at 17:30
  • These were the specifications Commented Jun 10, 2018 at 17:36
  • 3
    OK, then the specifications are generally broken for reasonable code. You have an answer below that will technically get you what you need. But I would resoundingly reject this code if it were submitted in a code review. Commented Jun 10, 2018 at 17:38
  • Agree. I did not write the specifications so I have been struggling for the last week. Commented Jun 10, 2018 at 17:41
  • If you need to access the function "b" which is inside another function "a" and you want to access it from the function "c", then you should define "b" in a scope that both "a" and "c" can see and use it. Commented Jul 3, 2021 at 12:27

3 Answers 3

6

When you do this, function b is defined locally within a. This means that it cannot be accessed by default outside of a. There are two main ways to solve this, but both involve modifying a:

  1. The global keyword (not recommended)

    def a():
        global b
        def b():
            print("hi")
    

    Here the global keyword sets b up as a global variable, so that you can then access it by calling it normally from within c. This is generally frowned upon.

  2. Returning the function from a and passing it to c

    def a():
        def b():
            print("hi")
        return b
    
    def c(b):
        #your code
    

    Then, when you call c, you should pass b to it, which a will have returned. You can either do so thus:

    b = a()
    c(b)
    

    Or you can simply call a every time you call c, thus:

    c(a())
    

    If you choose to do this, you can then define c thus:

    def c():
        b = a()
        #your code here
    

    which would allow you to simply call c normally, thus:

    `c()`
    
Sign up to request clarification or add additional context in comments.

1 Comment

This is exactly what I wanted to do but I am not allowed to edit a() or b() functions...I really don't see how it'll be possible without that return statement for b(). :(
1

This is not possible due to the way that Python scope works. b() is local to a(), and so does not exist within c().

EDIT: commenter is correct, the suggestion I initially gave doesn't work -- so this definitely just isn't possible.

1 Comment

Yeah I have been researching solutions for the past week and I am convinced it cannot be done. I was given a small assignment to do this..
0

As explained here, you cannot directly call a function included inside another. But, you can play around a bit and make it indirectly possible.

You could try calling functions via arguments and parameter values to call the function you want inside another function.

def a(invoke=None):
    def b():
       print('b')

    if invoke == 'b':
        b()


def c():
    a(invoke='b')

c()

The result is:

b

You can even pass arguments to these function if you want:

def a(invoke=None, a_parameter=None):
    def b(b_parameter=None):
        if b_parameter == None:
            print('b')
        if b_parameter == 'b1':
            print('b1')
        if b_parameter == 'b2':
            print('b2')

    if invoke == 'b':
        b(b_parameter = a_parameter)


def c(parameter=None):
    a(invoke='b', a_parameter = parameter)

c()

Result:

b

But:

c('b1')

Result:

b1

Comments

Your Answer

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