1

If I have two functions, (one inside the other);

def test1():
    def test2():
        print("test2")

How do I call test2?

5
  • You should clarify your question: do you mean how to call it from inside test1, or from outside? As the two answers show, the answer is either "the obvious way" or "you can't". Commented Apr 20, 2014 at 22:19
  • 1
    If you want to use test2 outside test1, you can return it like any other object - that's how decorators work. Commented Apr 20, 2014 at 22:22
  • Would I return it with return test2? @jonrsharpe Commented Apr 20, 2014 at 22:26
  • 1
    @zachgates yes, exactly. Functions are first-class in Python; they are just callable objects, you can pass them around like anything else. Commented Apr 20, 2014 at 22:27
  • 1
    What you have there is a function definition within a function definition. The inner one is executed every time the outer one is executed, and creates a new function every time. Which of the potentially infinite number of functions you want to call? Commented Apr 20, 2014 at 22:28

3 Answers 3

3

You can call it in this way too:

def test1():
    text = "Foo is pretty"
    print "Inside test1()"
    def test2():
        print "Inside test2()"
        print "test2() -> ", text
    return test2

test1()() # first way, prints "Foo is pretty"

test2 = test1() # second way
test2() # prints "Foo is pretty"

Let's see:

>>> Inside test1()
>>> Inside test2()
>>> test2() ->  Foo is pretty

>>> Inside test1()
>>> Inside test2()
>>> test2() ->  Foo is pretty

If you don't want to call the test2():

test1() # first way, prints "Inside test1()", but there's test2() as return value.
>>> Inside test1()
print test1()
>>> <function test2 at 0x1202c80>

Let's get more hard:

def test1():
    print "Inside test1()"
    def test2():
        print "Inside test2()"
        def test3():
            print "Inside test3()"
            return "Foo is pretty."
        return test3
    return test2

print test1()()() # first way, prints the return string "Foo is pretty."

test2 = test1() # second way
test3 = test2()
print test3() # prints "Foo is pretty."

Let's see:

>>> Inside test1()
>>> Inside test2()
>>> Inside test3()
>>> Foo is pretty.

>>> Inside test1()
>>> Inside test2()
>>> Inside test3()
>>> Foo is pretty.
Sign up to request clarification or add additional context in comments.

Comments

3
def test1():
  def test2():
    print "Here!"
  test2() #You need to call the function the usual way

test1() #Prints "Here!"

Note that the test2 function is not available outside of test1. If, for instance, you tried to call test2() later in your code, you would get an error.

4 Comments

What if I have more than one function inside test1?
@zachgates They can still only be called inside test1.
Any functions defined inside of test1 will only be available inside of test1. If you want to use the function later, you need to define it outside of test1
@sredmond: Or return it from test1.
0

You can't, since test2 stops existing once test1() has returned. Either return it from the outer function or only ever call it from there.

1 Comment

This is right if he means "how do I call test2 from outside test1", but not if he's asking how to call it from inside test1. (The question isn't clear on this.)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.