473,395 Members | 1,466 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Decorator question

I just discovered decorators. Very cool. My question is that I can't
figure out how to make a decorator not be restricted to a function so it
would also work on a method.

Here's my code:

def g(expr):
def rpt(func):
def wrapper(t):
for ii in range(expr):
print ii,
func(t)
wrapper.__name__ = func.__name__
wrapper.__dict__ = func.__dict__
wrapper.__doc__ = func.__doc__
return func
return wrapper
return rpt

@g(20)
def f(s):
print 's="%s"'%s
f('Hello')

It works fine, but now I want to apply the same decorator to a class
method.

class KK:
# @g(20) This obviously doesn't work.
def f(self, s):
print 's= %s'%s

k = KK()
k.f('Hello')

Is there a trick I need?

TIA

--
Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net
May 23 '07 #1
1 981
Steven W. Orr wrote:
I just discovered decorators. Very cool. My question is that I can't
figure out how to make a decorator not be restricted to a function so it
would also work on a method.

Here's my code:
@g(20)
def f(s):
print 's="%s"'%s
f('Hello')
Here you are calling f() with one string argument.
It works fine, but now I want to apply the same decorator to a class
method.

class KK:
# @g(20) This obviously doesn't work.
def f(self, s):
print 's= %s'%s

k = KK()
k.f('Hello')
Here you are calling KK.f() with two arguments (an implicit KK instance and
the explicit "Hello" string).

Both calls are channeled through wrapper(t) which expects exactly one
parameter.
Is there a trick I need?
Change wrapper() to accept an arbitrary number of arguments:
def g(expr):
def rpt(func):
def wrapper(*args):
for ii in range(expr):
print ii,
func(*args)
wrapper.__name__ = func.__name__
wrapper.__dict__ = func.__dict__
wrapper.__doc__ = func.__doc__
return func
return wrapper
return rpt
Peter

May 23 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Bengt Richter | last post by:
ISTM that @limited_expression_producing_function @another def func(): pass is syntactic sugar for creating a hidden list of functions. (Using '|' in place of '@' doesn't change the picture...
30
by: Ron_Adam | last post by:
I was having some difficulty figuring out just what was going on with decorators. So after a considerable amount of experimenting I was able to take one apart in a way. It required me to take a...
6
by: Schüle Daniel | last post by:
hello NG, consider this code >>> def timelogger(f): .... def wrapper(*a,**kw): .... print "started at %s" % time.ctime() .... t0 = time.time() .... ...
13
by: Lad | last post by:
I use Python 2.3. I have heard about decorators in Python 2.4. What is the decorator useful for? Thanks for reply L.
5
by: Doug | last post by:
I am looking at using the decorator pattern to create a rudimentary stored proc generator but am unsure about something. For each class that identifies a part of the stored proc, what if I want to...
1
by: Doug | last post by:
I am looking at using the decorator pattern to create a rudimentary stored proc generator but am unsure about something. For each class that identifies a part of the stored proc, what if I want to...
3
by: Gregory | last post by:
I recently reviewed the decorator pattern in the GOF book and noticed a problem. Let look at the example given in the book. For simplicity I removed intermediate Decorator class. // Interface...
9
by: Christian Hackl | last post by:
Hi! I've got a design question related to the combination of the NVI idiom (non-virtual interfaces, ) and popular object-oriented patterns such as Proxy or Decorator, i.e. those which have the...
4
by: thomas.karolski | last post by:
Hi, I would like to create a Decorator metaclass, which automatically turns a class which inherits from the "Decorator" type into a decorator. A decorator in this case, is simply a class which...
5
by: proxyuser | last post by:
The context of this question is actually from the book "C# 3.0 Design Patterns" (Bishop). She makes the point that one of the reasons you'd use Decorator is if you can't change the original...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.