L
L
Lesha2016-09-01 15:41:39
Python
Lesha, 2016-09-01 15:41:39

How to call a function that is in the body of another function?

Let's say I have a program:

def foo():
    # code
    def foo_in_foo():
        # code

How can you call the function foo_in_foo in the outer area of ​​the code?
# что-то вроде того:
foo.foo_in_foo()

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Andrey K, 2016-09-01
@enempluie

No way. You can't access a function's local variable, which is essentially the same thing.

D
DDDsa, 2016-09-01
@DDDsa

Why not, you can pull it out through a global variable.
Just don't remember this approach, it's terrible code:

>>> def foo():
... 	print('executing foo()')
... 	def foo_in_foo():
... 		print('executing foo_in_foo()')
... 	global inner
... 	inner = foo_in_foo
... 
>>> foo()
executing foo()
>>> inner
<function foo.<locals>.foo_in_foo at 0x07BBD1E0>
>>> inner()
executing foo_in_foo()

A
abcd0x00, 2016-09-02
@abcd0x00

You can do it, but you don't have to

>>> def foo():
...     print('in foo')
...     def foo_in_foo():
...         print('in foo_in_foo')
...     foo.foo_in_foo = foo_in_foo
... 
>>> foo()
in foo
>>> foo.foo_in_foo()
in foo_in_foo
>>>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question