As an example:
def get_booking(f=None): print "Calling get_booking Decorator" def wrapper(request, **kwargs): booking = _get_booking_from_session(request) if booking == None: # we don't have a booking in our session. return HttpRedirect('/') else: return f(request=request, booking=booking, **kwargs) return wrapper @get_booking def do_stuff(request, booking): # do stuff here
The problem I am having is, the @get_booking decorator is being called even before I called the function that I am decorating.
Output on start:
Calling get_booking Decorator Calling get_booking Decorator Calling get_booking Decorator Calling get_booking Decorator Calling get_booking Decorator Calling get_booking Decorator Calling get_booking Decorator Calling get_booking Decorator Calling get_booking Decorator Calling get_booking Decorator [26/Oct/2008 19:54:04] "GET /onlinebooking/?id=1,2 HTTP/1.1" 302 0 [26/Oct/2008 19:54:05] "GET /onlinebooking/ HTTP/1.1" 200 2300 [26/Oct/2008 19:54:05] "GET /site-media/css/style.css HTTP/1.1" 200 800 [26/Oct/2008 19:54:05] "GET /site-media/css/jquery-ui-themeroller.css HTTP/1.1" 200 25492
I haven’t even made a call to a function that is decorated at this point.
I am just getting started with decorators, so maybe I am missing something.
I believe python decorators are just syntactic sugar.
is the same thing as
As you can see, foo is being called even though bar has not been called. This is why you see the output from your decorator function. Your output should contain a single line for every function you applied your decorator to.