From: Yuccaplant on 29 Mar 2010 05:39 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
From: Peter Otten on 29 Mar 2010 05:53 Yuccaplant wrote: > 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(name): .... def always(f): .... def wrapper(self, *args, **kw): .... print attrgetter(name)(self) .... return f(self, *args, **kw) .... return wrapper .... return always .... >>> from operator import attrgetter >>> class A(object): .... def __init__(self, clk): .... self.clk = clk .... @always("clk.posedge") .... def hello(self): .... print "hello world" .... >>> class clk: .... posedge = 42 .... >>> A(clk).hello() 42 hello world There may be better ways depending on your usecase. Peter
|
Pages: 1 Prev: How to verify a signature using SHA1WithRSA with python Next: Python magazine |