I have a method that i have broken into smaller nested functions to break up the code base: Show
Is there a way to run one of the nested functions by itself. eg:
EDIT: I am trying to setup caching on a web server i have built using pyramid_breaker
This is my understanding may not be accurate: Now the reason i have this is that the namespace used by the decorator to create the cache key is genereated from the function and the arguements. You can't therefore just put the decorator on getThis as the request variable is unique-ish and the cache is useless. So i created the inner function which has repeatable args (search_term). However to invalidate the cache (ie refresh), the invalidation function requires scope to know of the 'getData' function so also needs to be nested. Therefore i need to call the nested function. You wonderful people have made it clear its not possible so is someone able to explain how i might do it with a different structure? asked Jun 22, 2012 at 10:41
user1474424user1474424 6571 gold badge5 silver badges18 bronze badges 4 I assume I suggest reworking the whole thing as a class. Something like this:
answered Jun 22, 2012 at 10:44
6 These previous answers, telling you that you can not do this, are of course wrong. This is python, you can do almost anything you want using some magic code magic. We can take the first constant out of foo's function code, this will be the see https://docs.python.org/2/library/new.html for more info on new and https://docs.python.org/2/library/inspect.html for more info on how to get to internal code. Warning: it's not because you CAN do this that you SHOULD do this, rethinking the way you have your functions structured is the way to go, but if you want a quick and dirty hack that will probably break in the future, here you go:
UPDATE: in python3 you can use the types module with
answered Oct 15, 2015 at 15:09
Jens TimmermanJens Timmerman 8,6871 gold badge39 silver badges46 bronze badges 4 There is,
you have to make them as an attribute of the function object. But this will work only after the first call of
answered Jun 22, 2012 at 10:52
mutantaculemutantacule 6,7031 gold badge26 silver badges39 bronze badges No (apart from poking around in closure objects, which is complete overkill here). If you need that, use a class.
Or just put those functions in the outer scope, since you're passing everything as arguments anyway:
answered Jun 22, 2012 at 10:44
Cat Plus PlusCat Plus Plus 122k26 gold badges195 silver badges222 bronze badges No, there is not. Since you may access variables in an outer scope from within a nested function:
there is no way to call If you must call answered Jun 22, 2012 at 10:44
Greg HewgillGreg Hewgill 908k177 gold badges1131 silver badges1267 bronze badges 1 You can try this way:
answered Aug 10, 2018 at 8:14
This is how I did it. CODE
OUTPUT
answered Jan 28, 2021 at 16:44
Anurag PAnurag P 3173 silver badges10 bronze badges How do you call nested functions?Approach:. Write one function inside another function.. Make a call to the inner function in the return statement of the outer function.. Call it fun(a)(b) where a is parameter to outer and b is to the inner function.. Finally return the combined output from the nested function.. 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!
How do you call one function inside another function in Python?The Fucntion1 Calls Function2 now the State of the Function1 is stored stack and execution of Function 1 will be continued when Function 2 returns. Consider the below Example of the function call. The Function SumOfSquares function calls the Function Square which returns the square of the number.
Can you have nested functions in Python?Inner functions, also known as nested functions, are functions that you define inside other functions. In Python, this kind of function has direct access to variables and names defined in the enclosing function. Inner functions have many uses, most notably as closure factories and decorator functions.
|