Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #8573
| Date | 2011-06-29 13:23 -0400 |
|---|---|
| From | John Posner <jjposner@codicesoftware.com> |
| Subject | Re: Using decorators with argument in Python |
| References | <d35e1e82-410b-471b-a3c7-379c1822adb0@m22g2000yqh.googlegroups.com> <4e0a0a8b$1@dnews.tpgi.com.au> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.502.1309370011.1164.python-list@python.org> (permalink) |
On 2:59 PM, Lie Ryan wrote:
>> Can any of you guys explain me advantages and disadvantages of
>> using each of them
> Simplicity is one, using @decor() means you have at least three-level
> nested functions, which means the code is likely to be very huge and
> perhaps unnecessarily.
Bruce Eckel pointed out (
http://www.artima.com/weblogs/viewpost.jsp?thread=240808) that the
result of "decoration" need not be a function. Instead, it can be an
object (instance of a user-defined class) that's callable (because the
class implements a __call__ method).
Investigating how this fact fit in with the current thread, I came up
with an alternative to the three levels of "def" (pronounced "three
levels of death"). Following is code for two decorators:
* the first one encloses the output of a function with lines of "#"
characters, and is used like this:
@enclose
myfun(...
* the second one encloses the output of a function with lines of a
user-specified character, and is used like this:
@enclose("&")
myfun(...
Here's the Python2 code for each one:
################## decorator to be called with no argument
class enclose:
"""
class that can be used as a function decorator:
prints a line of "#" before/after the function's output
"""
def __init__(self, funarg):
self.func = funarg
def __call__(self, *args, **kwargs):
print "\n" + "#" * 50
self.func(*args, **kwargs)
print "#" * 50 + "\n"
################## decorator to be called with argument
def enclose(repeat_char):
"""
function that returns a class that can be used as a decorator:
prints a line of <repeat_char> before/after the function's output
"""
class _class_to_use_as_decorator:
def __init__(self, funarg):
self.func = funarg
def __call__(self, *args, **kwargs):
print "\n" + repeat_char * 50
self.func(*args, **kwargs)
print repeat_char * 50 + "\n"
return _class_to_use_as_decorator
Best,
John
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Using decorators with argument in Python Jigar Tanna <poisonousrattle5@gmail.com> - 2011-06-28 09:52 -0700
Re: Using decorators with argument in Python Lie Ryan <lie.1296@gmail.com> - 2011-06-29 03:06 +1000
Re: Using decorators with argument in Python John Posner <jjposner@codicesoftware.com> - 2011-06-29 13:23 -0400
Re: Using decorators with argument in Python Ethan Furman <ethan@stoneleaf.us> - 2011-06-29 12:30 -0700
Re: Using decorators with argument in Python Ian Kelly <ian.g.kelly@gmail.com> - 2011-06-29 13:29 -0600
Re: Using decorators with argument in Python Ethan Furman <ethan@stoneleaf.us> - 2011-06-29 14:29 -0700
Re: Using decorators with argument in Python Ian Kelly <ian.g.kelly@gmail.com> - 2011-06-29 15:26 -0600
Re: Using decorators with argument in Python Ethan Furman <ethan@stoneleaf.us> - 2011-06-29 14:51 -0700
Re: Using decorators with argument in Python Duncan Booth <duncan.booth@invalid.invalid> - 2011-06-30 09:00 +0000
Re: Using decorators with argument in Python John Posner <jjposner@codicesoftware.com> - 2011-07-01 11:18 -0400
Re: Using decorators with argument in Python Ian Kelly <ian.g.kelly@gmail.com> - 2011-06-28 11:20 -0600
Re: Using decorators with argument in Python Ben Finney <ben+python@benfinney.id.au> - 2011-06-29 08:19 +1000
csiph-web