Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing a function within a function(nested function?) [duplicate]

Tags:

python

Python noob here. How do I get hold of the 'inner' function within the 'fib' function?

from time import sleep

class Fibonacci(object):

    def __init__(self, a, b, limit=50):
        self.a = a
        self.b = b
        self.limit = limit

    def fib(self):

        while self.a < self.limit:
            c = self.a + self.b
            sleep(1)
            print self.a,
            self.b = self.a
            self.a = c

        def inner(self):
            print 'Damn it! Just print already!'


j = Fibonacci(0,1,2)
j.fib()

## This doesn't work. Gives an "AttibuteError: 'function' object has no attribute 'inner'"
j.fib.inner()
like image 354
kassold Avatar asked Aug 14 '11 00:08

kassold


People also ask

Can you access inner function Python?

You cannot, not unless fib returns inner somehow. inner is essentially a local variable inside the scope of fib and you can't access a function's locals from outside of it.

How do you call nest inner function in Python?

Defining an Inner Function In the above example, function2() has been defined inside function1() , making it an inner function. To call function2() , we must first call function1() . The function1() will then go ahead and call function2() as it has been defined inside it. The code will return nothing when executed!

Can we define function inside function in Python?

A function defined inside another function is called a nested function. Nested functions can access variables of the enclosing scope. In Python, these non-local variables are read-only by default and we must declare them explicitly as non-local (using nonlocal keyword) in order to modify them.


2 Answers

You cannot, not unless fib returns inner somehow. inner is essentially a local variable inside the scope of fib and you can't access a function's locals from outside of it. (That wouldn't even make sense, since the locals don't exist except when the function is running. Think about it -- would it make sense to access fib's c variable from outside of the function?)

like image 193
cdhowie Avatar answered Oct 16 '22 18:10

cdhowie


Do not use the following.

[...]
>>> j = Fibonacci(0,1,2) 
>>> j.fib()
0 1 1
>>> # dark magic begins!
>>> import new
>>> new.function(j.fib.im_func.func_code.co_consts[2],{})(None)
Damn it! Just print already!

You can tell simply by looking at it that it's not really Python, and for that matter it isn't really calling the "inner" function itself, it's simply creating a new function like it. I also didn't bother setting the globals 'correctly', because this is a terrible thing to do in the first place..

[I should mention that the point of the above is to note that the idea that you can't access internals from outside isn't strictly true, though it's almost never a good idea. Exceptions include interpreter-level code inspections, etc.]

Unclean! Unclean!

like image 45
DSM Avatar answered Oct 16 '22 17:10

DSM