The Artima Developer Community
Sponsored Link

Weblogs Forum
Python Decorators II: Decorator Arguments

19 replies on 2 pages. Most recent reply: Feb 9, 2012 6:47 PM by Jake Griffin

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 19 replies on 2 pages [ « | 1 2 ]
Bruce Eckel

Posts: 874
Nickname: beckel
Registered: Jun, 2003

Re: Python Decorators II: Decorator Arguments Posted: Oct 26, 2008 12:46 PM
Reply to this message Reply
Advertisement
> But that's just history.

I'll just point out that this statement could be applied to anything in order to dismiss it. Including underscores.

Ian Bicking

Posts: 849
Nickname: ianb
Registered: Apr, 2003

Re: Python Decorators II: Decorator Arguments Posted: Oct 26, 2008 1:05 PM
Reply to this message Reply
> > But that's just history.

> I'll just point out that this statement could be applied to anything in order to dismiss it. Including underscores.

Of course... the point I was making is that there's nothing really important about the different styles. None of them are intrinsically better than the others. The only important part of it is consistency, and to be consistent with most Python code you should use underscores.

Guido van van Rossum

Posts: 359
Nickname: guido
Registered: Apr, 2003

Re: Python Decorators II: Decorator Arguments Posted: Oct 26, 2008 2:17 PM
Reply to this message Reply
I wonder if, when first introducing the decorator with arguments, it might be better to use a wrapping function around a class instead of the other way around. That way the class can look pretty much the same as the class used for the argument-less decorator, which will make the reader immediately understand what's different in this example.

Jake Griffin

Posts: 2
Nickname: jako7286
Registered: Feb, 2012

Re: Python Decorators II: Decorator Arguments Posted: Feb 9, 2012 6:46 PM
Reply to this message Reply
I prefer an alternate form of your "decorator with arguments" class implementation that I would like to share with you. When I see this:

@decoratorWithArguments("Hello", "World", 42)

I think of this as a function that RETURNS a decorator as opposed to a decorator class that returns a function. That way, you don't need as much of a paradigm shift when using vs not using arguments, while still getting the benefit of not having to explicitly save arg1, arg2, and arg3. Here is how your example would be implemented using this concept:

NOTE: The strings that are printed do not exactly make sense with my implementation, but I left them like this to show the parallel to your implementation. The output of this program is identical to yours.

def decoratorWithArguments(arg1, arg2, arg3):
__print "Inside __init__():"
__class wrapped_f(object):
____def __init__(self, f):
______print "Inside __call__()"
______self.f = f
____def __call__(self, *args):
______print "Inside wrapped_f()"
______print "Decorator arguments:", arg1, arg2, arg3
______self.f(*args)
______print "After f(*args)"
__return decoratingClass

@decoratorWithArguments("Hello", "world", 42)
def sayHello(a1, a2, a3, a4):
__print 'sayHello arguments:', a1, a2, a3, a4

print "After decoration"
print "Preparing to call sayHello()"
sayHello("say", "hello", "argument", "list")
print "after first sayHello() call"
sayHello("a", "different", "set of", "arguments")
print "after second sayHello() call"

Jake Griffin

Posts: 2
Nickname: jako7286
Registered: Feb, 2012

Re: Python Decorators II: Decorator Arguments Posted: Feb 9, 2012 6:47 PM
Reply to this message Reply
the return from the function should be

return wrapped_f

Renamed to match your example but missed that one.

Flat View: This topic has 19 replies on 2 pages [ « | 1  2 ]
Topic: Scrum will never be the same Previous Topic   Next Topic Topic: WebSockets: A Glimpse of the Future


Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us