Problem with decorator - python

def my_decorator(func) :
print "I am a ordinary function"
def wrapper() :
print "I am function returned by the decorator"
func()
return wrapper
def lazy_function() :
print "zzzzzzzz"
functionDecorator=my_decorator(lazy_function)
I have in Debug IO
I am a ordinary function
I don't get it why there is any output. I understand why this line:
functionDecorator()
returns me:
I am function returned by the decorator
another___zzzzzzzz
But for the rest I think I miss something

When you first call my_decorator, before it returns the the function wrapper, it will print your message.
Try calling functionDecorator() again, you won't get the output a second time because the function has already been decorated by my_decorator. Each time you call my_decorator, however, it will print your message, because it is a normal function call (that happens to also return a decorated function).
Also, see http://www.python.org/dev/peps/pep-0318/#current-syntax for alternative syntax for implementing decorators.

Decorator is a normal function, that takes another function and returns some modified function.
When you call:
functionDecorator=my_decorator(lazy_function)
code inside your decorator is instantly executed and so print statement is executed, new function is created and returned. Then, when you run decorated function (which is simply a new function built inside a decorator), only internal print is executed and the external is not executed ever again. Is that clear?

Related

Python inner functions/decorators: When should I use parentheses when returning an inner function?

I am learning about Python decorators and inner functions and have some questions about the lesson I'm learning via a YouTube video from codeacademy.com https://youtu.be/WOHsHaaJ8VQ.
When using inner functions sometimes I have to return the function with parenthesis, and sometimes without.
If I call an inner function without using decorators, I have to use parentheses when returning the inner function, otherwise it seems the inner function is returned as an object(?).
In the YouTube video from codeacademy.com as well as this one https://www.youtube.com/watch?v=FsAPt_9Bf3U, they call the inner function without parentheses and the expected result is output.
If I call an inner function using decorators, I have to not use parentheses when returning the inner function, otherwise it seems to work correctly, but throws an error along with some other weird results.
I've written some code to test different variations and output the results.
You can see the live code here: https://trinket.io/python/af1b47658f
# Test 1: The title function returns inner function wrapper without parentheses.
def title(print_name_function):
def wrapper():
print("Professor:")
print_name_function()
return wrapper # Without parentheses
def print_my_name():
print("John")
print('Test 1')
title(print_my_name)
# Results: Nothing is printed.
# Test 2: The title function returns inner function wrapper with parentheses.
def title(print_name_function):
def wrapper():
print("Professor:")
print_name_function()
return wrapper() # With parentheses
def print_my_name():
print("John")
print('Test 2')
title(print_my_name)
# Results: Professor John is printed.
# Test 3: Using a decorator while the title function returns inner function wrapper without parentheses
def title(print_name_function):
def wrapper():
print("Professor:")
print_name_function()
return wrapper # Without parentheses
#title
def print_my_name():
print("John")
print('Test 3')
print_my_name()
# Results: Professor John is printed.
# Test 4: Using a decorator while the title function returns inner function wrapper with parentheses
def title(print_name_function):
def wrapper():
print("Professor:")
print_name_function()
return wrapper() # With parentheses
#title
def print_my_name():
print("John")
print('Test 4')
print_my_name()
# Results: Professor John is printed and the following error is thrown:
'''
Traceback (most recent call last):
File "decorators.py", line 59, in <module>
print_my_name()
TypeError: 'NoneType' object is not callable.
'''
# Additionally, Professor John is printed before 'Test 4' is printed which seems that print_my_name() runs, then print('Test 4') runs.
In the two videos I've watched listed above about inner functions/decorators I've found...
For inner functions: the inner function was returned without using parentheses and ran correctly. Upon my testing, I have to use the parentheses for it to run correctly.
For decorators: the inner function was returned without using parentheses and ran correctly. Upon my testing, running without using parentheses works. Running with parentheses seems to work, but the output order is mixed up and an error is thrown (see test 4 in my code).
Let's break this down into two parts.
1) Let's ignore decorators for now.
You should use parentheses when you want to call some function.
Without parentheses, a function is just its name.
For example:
Here is a function, where we give it a number, and we get back that number plus 5.
def add_five(x):
return x + 5
We see that add_five, without parentheses, is just the function definition. Think of it as a recipe. It's not the actually cake, just the instructions on how to bake a cake.
>>> add_five
<function add_five at 0x10da3ce18>
Now we give it an ingredient, and it makes a cake:
>>> add_five(1)
6
Let's do a similar thing, but with better names.
>>> def make_cake(cake_type):
>>> print("Making: " + cake_type + " cake!")
>>> make_cake("carrot")
'Making: carrot cake!'
>>> make_cake
<function make_cake at 0x10da3cf28>
Ok, so when we put the function name without any parentheses, we're not actually calling the function, we're just getting the declaration of the function (which is kinda like the function's Birth Certificate, which has its memory address, in this case: 0x10da3cf28.
The same thing applies for functions that don't expect any parameters.
Without the parentheses, you're just asking, "Hey function, you exist?"
With the parentheses (and necessary parameters/variables required), you're saying, "Hey function, do something!"
Now for the second part.
2) What do Decorators do?
#SyntaxVoid has a great explanation about what you're doing. Decorators are a much more complicated thing, so I'll stick to explaining what they're doing in this specific context.
Basically, your decorator, #<Some Function Name> specifies a function to call the decorated function on.
def some_decorator(function_that_I_decorated):
print("I'm going to print this, and then call my decorated function!")
function_that_I_decorated()
#some_decorator
def my_decorated_function():
print("Did I do anything?")
Then we see the results:
>>> def some_decorator(function_that_I_decorated):
... print("I'm going to print this, and then call my decorated function!")
... function_that_I_decorated()
...
>>> #some_decorator
... def my_decorated_function():
... print("Did I do anything?")
...
I'm going to print this, and then call my decorated function!
Did I do anything?
Now here's the important part:
>>> my_decorated_function
>>> my_decorated_function()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable
Wait... didn't we define my_decorated_function?
Yes, we defined the function, but the decorator is reassigning that function name to something else.
Namely, my_decorator_function = some_decorator(my_decorator_function)
Now some_decorator happens to do something before calling my_decorator_function. It prints some stuff. But what is the return value of some_decorator? There's no return statement, so some_decorator returns None by default.
Therefore, my_decorator_function was created, run, and now has a new value.
Why would we want this behavior?
When we want the output to change, when running the same function with the same input(s) multiple times.
For example, maybe I want a function that returns "Go Left" every other time it's called, or "Go Right" every 5-times the function gets called.
If I want to do this with a function with more than one variable, that's easy! Just pass it in and check if num_times == whatever_int.
But life ain't so easy- sometimes other people already have functions written that are much simpler and only allow one variable, because that's more generalizable. Or maybe it's so complex it would take us a really long time to figure out how the function works (and we usually don't want to violate abstraction barriers anyways). In those situations, we need to adapt their function to our needs.
I would encourage you to read more about Currying, as that'll help you understand other uses too.
Let me use this famous quote first.
In python everything is an object.
I've been wrapping my head about two hours, until I remembered this quote. We should think original function and decorated function as objects, given an example:
def decorator(original_func):
def decorated_func():
print("Start decoration")
original_func()
print("End decoration")
return decorated_func # return decorated function as object without calling
#decorator
def func():
print("I will be decorated")
func()
The decorator_func transfers the func object to decorated_func object, and return the decorated_func as an object, so when we call func object with it's original name, we are actually calling the new function object decorated_func, which is equivalent to decorated_func().
Now it is easy to see why return decorated_func() is wrong, if we return decorated_func() in definition of decorator, we are return None, because the default return value of a function is None, and None is not callable as traceback says, so we can't use func() to call func.
Additionally, although the following two codes are equivalent, decorator help us to simplify our code, and without changing the original function object, also don't need to create a new function object mannually
#decorator
def func():
print("I will be decorated")
func()
def func():
print("I will be decorated")
dec_func = decorator(func)
dec_func()

Using function before declaration in Python [duplicate]

Is it possible to forward-declare a function in Python? I want to sort a list using my own cmp function before it is declared.
print "\n".join([str(bla) for bla in sorted(mylist, cmp = cmp_configs)])
I've put the definition of cmp_configs method after the invocation. It fails with this error:
NameError: name 'cmp_configs' is not defined
Is there any way to "declare" cmp_configs method before it's used?
Sometimes, it is difficult to reorganize code to avoid this problem. For instance, when implementing some forms of recursion:
def spam():
if end_condition():
return end_result()
else:
return eggs()
def eggs():
if end_condition():
return end_result()
else:
return spam()
Where end_condition and end_result have been previously defined.
Is the only solution to reorganize the code and always put definitions before invocations?
Wrap the invocation into a function of its own so that
foo()
def foo():
print "Hi!"
will break, but
def bar():
foo()
def foo():
print "Hi!"
bar()
will work properly.
The general rule in Python is that a function should be defined before its usage, which does not necessarily mean it needs to be higher in the code.
If you kick-start your script through the following:
if __name__=="__main__":
main()
then you probably do not have to worry about things like "forward declaration". You see, the interpreter would go loading up all your functions and then start your main() function. Of course, make sure you have all the imports correct too ;-)
Come to think of it, I've never heard such a thing as "forward declaration" in python... but then again, I might be wrong ;-)
If you don't want to define a function before it's used, and defining it afterwards is impossible, what about defining it in some other module?
Technically you still define it first, but it's clean.
You could create a recursion like the following:
def foo():
bar()
def bar():
foo()
Python's functions are anonymous just like values are anonymous, yet they can be bound to a name.
In the above code, foo() does not call a function with the name foo, it calls a function that happens to be bound to the name foo at the point the call is made. It is possible to redefine foo somewhere else, and bar would then call the new function.
Your problem cannot be solved because it's like asking to get a variable which has not been declared.
I apologize for reviving this thread, but there was a strategy not discussed here which may be applicable.
Using reflection it is possible to do something akin to forward declaration. For instance lets say you have a section of code that looks like this:
# We want to call a function called 'foo', but it hasn't been defined yet.
function_name = 'foo'
# Calling at this point would produce an error
# Here is the definition
def foo():
bar()
# Note that at this point the function is defined
# Time for some reflection...
globals()[function_name]()
So in this way we have determined what function we want to call before it is actually defined, effectively a forward declaration. In python the statement globals()[function_name]() is the same as foo() if function_name = 'foo' for the reasons discussed above, since python must lookup each function before calling it. If one were to use the timeit module to see how these two statements compare, they have the exact same computational cost.
Of course the example here is very useless, but if one were to have a complex structure which needed to execute a function, but must be declared before (or structurally it makes little sense to have it afterwards), one can just store a string and try to call the function later.
If the call to cmp_configs is inside its own function definition, you should be fine. I'll give an example.
def a():
b() # b() hasn't been defined yet, but that's fine because at this point, we're not
# actually calling it. We're just defining what should happen when a() is called.
a() # This call fails, because b() hasn't been defined yet,
# and thus trying to run a() fails.
def b():
print "hi"
a() # This call succeeds because everything has been defined.
In general, putting your code inside functions (such as main()) will resolve your problem; just call main() at the end of the file.
There is no such thing in python like forward declaration. You just have to make sure that your function is declared before it is needed.
Note that the body of a function isn't interpreted until the function is executed.
Consider the following example:
def a():
b() # won't be resolved until a is invoked.
def b():
print "hello"
a() # here b is already defined so this line won't fail.
You can think that a body of a function is just another script that will be interpreted once you call the function.
Sometimes an algorithm is easiest to understand top-down, starting with the overall structure and drilling down into the details.
You can do so without forward declarations:
def main():
make_omelet()
eat()
def make_omelet():
break_eggs()
whisk()
fry()
def break_eggs():
for egg in carton:
break(egg)
# ...
main()
# declare a fake function (prototype) with no body
def foo(): pass
def bar():
# use the prototype however you see fit
print(foo(), "world!")
# define the actual function (overwriting the prototype)
def foo():
return "Hello,"
bar()
Output:
Hello, world!
No, I don't believe there is any way to forward-declare a function in Python.
Imagine you are the Python interpreter. When you get to the line
print "\n".join([str(bla) for bla in sorted(mylist, cmp = cmp_configs)])
either you know what cmp_configs is or you don't. In order to proceed, you have to
know cmp_configs. It doesn't matter if there is recursion.
You can't forward-declare a function in Python. If you have logic executing before you've defined functions, you've probably got a problem anyways. Put your action in an if __name__ == '__main__' at the end of your script (by executing a function you name "main" if it's non-trivial) and your code will be more modular and you'll be able to use it as a module if you ever need to.
Also, replace that list comprehension with a generator express (i.e., print "\n".join(str(bla) for bla in sorted(mylist, cmp=cmp_configs)))
Also, don't use cmp, which is deprecated. Use key and provide a less-than function.
Import the file itself. Assuming the file is called test.py:
import test
if __name__=='__main__':
test.func()
else:
def func():
print('Func worked')
TL;DR: Python does not need forward declarations. Simply put your function calls inside function def definitions, and you'll be fine.
def foo(count):
print("foo "+str(count))
if(count>0):
bar(count-1)
def bar(count):
print("bar "+str(count))
if(count>0):
foo(count-1)
foo(3)
print("Finished.")
recursive function definitions, perfectly successfully gives:
foo 3
bar 2
foo 1
bar 0
Finished.
However,
bug(13)
def bug(count):
print("bug never runs "+str(count))
print("Does not print this.")
breaks at the top-level invocation of a function that hasn't been defined yet, and gives:
Traceback (most recent call last):
File "./test1.py", line 1, in <module>
bug(13)
NameError: name 'bug' is not defined
Python is an interpreted language, like Lisp. It has no type checking, only run-time function invocations, which succeed if the function name has been bound and fail if it's unbound.
Critically, a function def definition does not execute any of the funcalls inside its lines, it simply declares what the function body is going to consist of. Again, it doesn't even do type checking. So we can do this:
def uncalled():
wild_eyed_undefined_function()
print("I'm not invoked!")
print("Only run this one line.")
and it runs perfectly fine (!), with output
Only run this one line.
The key is the difference between definitions and invocations.
The interpreter executes everything that comes in at the top level, which means it tries to invoke it. If it's not inside a definition.
Your code is running into trouble because you attempted to invoke a function, at the top level in this case, before it was bound.
The solution is to put your non-top-level function invocations inside a function definition, then call that function sometime much later.
The business about "if __ main __" is an idiom based on this principle, but you have to understand why, instead of simply blindly following it.
There are certainly much more advanced topics concerning lambda functions and rebinding function names dynamically, but these are not what the OP was asking for. In addition, they can be solved using these same principles: (1) defs define a function, they do not invoke their lines; (2) you get in trouble when you invoke a function symbol that's unbound.
Python does not support forward declarations, but common workaround for this is use of the the following condition at the end of your script/code:
if __name__ == '__main__': main()
With this it will read entire file first and then evaluate condition and call main() function which will be able to call any forward declared function as it already read the entire file first. This condition leverages special variable __name__ which returns __main__ value whenever we run Python code from current file (when code was imported as a module, then __name__ returns module name).
"just reorganize my code so that I don't have this problem." Correct. Easy to do. Always works.
You can always provide the function prior to it's reference.
"However, there are cases when this is probably unavoidable, for instance when implementing some forms of recursion"
Can't see how that's even remotely possible. Please provide an example of a place where you cannot define the function prior to it's use.
Now wait a minute. When your module reaches the print statement in your example, before cmp_configs has been defined, what exactly is it that you expect it to do?
If your posting of a question using print is really trying to represent something like this:
fn = lambda mylist:"\n".join([str(bla)
for bla in sorted(mylist, cmp = cmp_configs)])
then there is no requirement to define cmp_configs before executing this statement, just define it later in the code and all will be well.
Now if you are trying to reference cmp_configs as a default value of an argument to the lambda, then this is a different story:
fn = lambda mylist,cmp_configs=cmp_configs : \
"\n".join([str(bla) for bla in sorted(mylist, cmp = cmp_configs)])
Now you need a cmp_configs variable defined before you reach this line.
[EDIT - this next part turns out not to be correct, since the default argument value will get assigned when the function is compiled, and that value will be used even if you change the value of cmp_configs later.]
Fortunately, Python being so type-accommodating as it is, does not care what you define as cmp_configs, so you could just preface with this statement:
cmp_configs = None
And the compiler will be happy. Just be sure to declare the real cmp_configs before you ever invoke fn.
Python technically has support for forward declaration.
if you define a function/class then set the body to pass, it will have an empty entry in the global table.
you can then "redefine" the function/class later on to implement the function/class.
unlike c/c++ forward declaration though, this does not work from outside the scope (i.e. another file) as they have their own "global" namespace
example:
def foo(): pass
foo()
def foo(): print("FOOOOO")
foo()
foo is declared both times
however the first time foo is called it does not do anything as the body is just pass
but the second time foo is called. it executes the new body of print("FOOOOO")
but again. note that this does not fix circular dependancies. this is because files have their own name and have their own definitions of functions
example 2:
class bar: pass
print(bar)
this prints <class '__main__.bar'> but if it was declared in another file it would be <class 'otherfile.foo'>
i know this post is old, but i though that this answer would be useful to anyone who keeps finding this post after the many years it has been posted for
One way is to create a handler function. Define the handler early on, and put the handler below all the methods you need to call.
Then when you invoke the handler method to call your functions, they will always be available.
The handler could take an argument nameOfMethodToCall. Then uses a bunch of if statements to call the right method.
This would solve your issue.
def foo():
print("foo")
#take input
nextAction=input('What would you like to do next?:')
return nextAction
def bar():
print("bar")
nextAction=input('What would you like to do next?:')
return nextAction
def handler(action):
if(action=="foo"):
nextAction = foo()
elif(action=="bar"):
nextAction = bar()
else:
print("You entered invalid input, defaulting to bar")
nextAction = "bar"
return nextAction
nextAction=input('What would you like to do next?:')
while 1:
nextAction = handler(nextAction)

Why and when does the wrapper function call itself rather than the original_function passed to the decorator function?

I'm a beginner at Python and I came across this issue while declaring decorators. I don't know what to call this specific scenario so I'm unable to google it effectively.
The scenario is that I declared 2 decorator functions, decorating_function1 and decorating_function2 and added them to my function named original_function. When I executed the original_function, it went into an infinite recursion. After debugging I found that the wrapper function of decorating_function2 was calling itself rather than the original_function that was passed to decorating_function2.
Furthermore, when I tried this code directly on the Python interpreter, it ran without any issues. (I'm unable to reproduce this scenario in the interpreter)
So, my question is, is there a specific scenario that can result in this type of behavior?
Code snippet of what I was trying to do. The code below runs correctly:
def decorating_function1(original_function):
print(original_function.__name__)
def wrapper(*vargs,**kwargs):
print('inside wrapper of df1')
return original_function(*vargs,**kwargs)
return wrapper
def decorating_function2(original_function):
print(original_function.__name__)
def wrapper(*vargs,**kwargs):
print('inside wrapper of df2')
return original_function(*vargs,**kwargs)
return wrapper
#decorating_function2
#decorating_function1
def original_function(name,age):
print("Name of the person is %s and his age is %d" % (name,age))
EDIT:
I haven't been able to reproduce the scenario. I was using Emacs when this issue had occurred. I've tried to reproduce this issue with other editors and the python interpreter in vain. So, I'm assuming this issue is not that major and shouldn't be possible normally.
Your code works for me fine, so I'll address the question about the calling order here.
You may have heard that "stacked" decorators are called "bottom-up". What does this mean?
When your call original_function, then decorating_function1 is called with the function as the argument. Now, this returns wrapper. Then, you have decorating_function2 "stacked" on top of decorating_function1 after this finishes.
Hence, decorating_function2 is called with an argument of wrapper. Then, this executes... so wrapper inside of decorating_function2 is executed which in turn calls decorating_function1's wrapper which then calls original_function.
So, it looks like this:
original_function (e.g. original_function(age, name) )
i) decorating_function1 -> returns "wrapper"
ii) decorating_function2(wrapper) -> returns "wrapper"
wrapper for ii) executed which is calling i)'s wrapper hence, i)
is executed and this calls original_function
If you were to add the extra bit "decorating_function1/2" before original_function.__name__ in your wrappers you would see this:
Decorating function1 original_function
Decorating function2 wrapper
inside wrapper of df2
inside wrapper of df1
Name of the person is (a name) and his age is (a age)

Python decorator function execution

I have below decorator demonstration code. If I execute it without explicitly calling greet function, it is executing print statement inside decorator function and outputs Inside decorator.
I am unable to understand this behavior of decorator. How the time_decorator is called even if I didn't call greet function?
I am using Python 3.
def time_decorator(original_func):
print('Inside decorator')
def wrapper(*args, **kwargs):
start = time.clock()
result = original_func(*args, **kwargs)
end = time.clock()
print('{0} is executed in {1}'.format(original_func.__name__, end-start))
return result
return wrapper
#time_decorator
def greet(name):
return 'Hello {0}'.format(name)
Decorators are called at start time (when the python interpreter reads the code as the program starts), not at runtime (when the decorated function is actually called).
At runtime, it is the wrapped function wrapper which is called and which itself calls the decorated function and returns its result.
So this is totally normal that the print line gets executed.
If, i.e, you decorate 10 functions, you will see 10 times the print output. No need to even call the decorated functions for this to happen.
Move the print inside wrapper and this won't happen anymore.
Decorators as well as metaclasses are part of what is called meta-programming (modify / create code, from existing code). This is a really fascinating aspect of programming which takes time to understand but offers amazing possibilities.
time_decorator is executed during function decoration. wrapper is not (this is function invoked when decorated greet() is called).
# is just syntactic sugar. Following code snippets are equivalent.
Decorator syntax:
#time_decorator
def greet(name):
return 'Hello {0}'.format(name)
Explicit decoration process - decorator is a function that returns new function based on another one.
def greet(name):
return 'Hello {0}'.format(name)
greet = time_decorator(greet)

Function called by the control flow, not by tkinter bind

I have a following function
def insert_word(listbox,text):
t_start.insert(INSERT, text)
print "worked"
binded to "< Return >" key via
listbox.bind("<Return>", insert_word(t_start,listbox.get(ACTIVE)))
Why the function is being called when the control flow comes, not when I press Return?
What's the entire idea behind bind function if it can be triggered somehow else then the bind itself?
Would I need a class with __init__ and __call__ methods to resolve this?
The function is called because you are actually calling it.
listbox.bind("<Return>", insert_word(t_start,listbox.get(ACTIVE)))
# ^----this function call is evaluated---^
What you want to do is to provide bind with a callback, that is a function object. You can use a closure do do this.
def callback(t_start, text):
def inner():
t_start.insert(INSERT, text)
return inner # Return the function
listbox.bind("<Return>", callback(t_start, listbox.get(ACTIVE)) )
# ^----this call returns a function----^
# Be aware that ^--this parameter-^ is
# still evaluated when the interpreter
# evaluates the statement
The callback function will be called when the event is triggered.
Like #ddelemeny said, that function is going to be called as it is written. If your program was structured into classes, you normally wouldn't need to pass an argument, because you can interact with variables directly from the function. However, a simple solution for your case would be using a lambda expression, so Python won't call the callback function when the control flow reaches it.
listbox.bind("<Return>", lambda e: insert_word(t_start,listbox.get(ACTIVE)))
http://effbot.org/zone/tkinter-callbacks.htm

Categories