refer to self in a decorator on a class method

Y

Yuccaplant

Hi all,

I would like to do something like this:

********************
class HelloWorld (object):

def __init__(self,clk):
self.clk = clk

@always(self.clk.posedge)
def sayHello(self):
print "%s Hello World!" % now()
********************

Problem is however I can't refer to self in the decorator call. Is
there any workarround?

thanks a lot,

pieter
 
P

Peter Otten

Yuccaplant said:
Hi all,

I would like to do something like this:

********************
class HelloWorld (object):

def __init__(self,clk):
self.clk = clk

@always(self.clk.posedge)
def sayHello(self):
print "%s Hello World!" % now()
********************

Problem is however I can't refer to self in the decorator call. Is
there any workarround?

Pass attribute names:
.... def always(f):
.... def wrapper(self, *args, **kw):
.... print attrgetter(name)(self)
.... return f(self, *args, **kw)
.... return wrapper
.... return always
........ def __init__(self, clk):
.... self.clk = clk
.... @always("clk.posedge")
.... def hello(self):
.... print "hello world"
........ posedge = 42
....42
hello world

There may be better ways depending on your usecase.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,225
Latest member
Top Crypto Podcasts

Latest Threads

Top