473,811 Members | 2,756 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

is decorator the right thing to use?

Hi,

after hearing a lot about decorators and never actually using one I have
decided to give it a try. My particular usecase is that I have class that
acts as a proxy to other classes (i.e. passes messages along to those
classes) however hand-coding this type of class is rather tedious, so I
decided to use decorator for that. Can somebody tell me if what I'm doing
is a potential shot-in-the-foot or am I on the right track? (Note, It's
rather rudimentary proof-of-concept implementation and not the final
solution I'm about to employ so there are no optimizations or
signature-preserving code there yet, just the idea).

Here's the code:

class A:
b=None
def __init__(self,b ):
self.val='aval'
self.b=b
b.val='aval'

def mymethod(self,a ):
print "A::mymetho d, ",a

def mymethod2(self, a):
print "A::another method, ",a
def Aproxy(fn):
def delegate(*args, **kw):
print "%s::%s" % (args[0].__class__.__na me__,fn.__name_ _)
args=list(args)
b=getattr(args[0],'b')
fnew=getattr(b, fn.__name__)
# get rid of original object reference
del args[0]
fnew(*args,**kw )
setattr(A,fn.__ name__,delegate )
return fn

class B:
def __init__(self):
self.val='bval'

@Aproxy
def bmethod(self,a) :
print "B::bmethod "
print a, self.val

@Aproxy
def bmethod2(self,a ):
print "B::bmethod 2"
print a, self.val

b=B()
b.bmethod('foo' )
a=A(b)
b=B()
b.val='newval'
a.bmethod('bar' )
a.bmethod2('zam ')
Sep 24 '08
25 1459
On Sep 25, 3:36 pm, "Dmitry S. Makovey" <dmi...@athabas cau.cawrote:
Aaron "Castironpi " Brady wrote:
I kept this part of the problem out of this discussion as I'm pretty sure
I can fill those in once I figure out the basic problem of
auto-population of proxy methods since for each class/method those are
going to be nearly identical. If I can autogenerate those on-the-fly I'm
pretty sure I can add some extra-logic to them as well including
signature change where A::bmethod(self ,c,x) would become
A::bmethod(self ,x) etc.
Do you want to couple instances or classes together?

It would be nice to have objects of B, C and D classes not knowing that they
are proxied (as they are used on their own too, not only inside of A
objects).
I'm not sure if the approach below deals with all the issues, but one
thing it does is decouple completely the proxied objects from the
proxy:

#======== usage
=============== =============== =============== =============== ====

from proxies import Proxy

class B(object):
def __init__(self): self.val = 'bval'
def bmethod(self,n) : print "B::bmethod ",n
def bmethod2(self,n ,m): print "B::bmethod2",n ,m

class C(object):
def __init__(self): self.val = 'cval'
def cmethod(self,x) : print "C::cmethod ",x
def cmethod2(self,x ,y): print "C::cmethod2",x ,y
cattr = 4

class A(Proxy):
DelegateMap = {
'bmethod' : B,
'bmethod2': B,
'cmethod': C,
# do NOT delegate C.cmethod2
#'cmethod2': C,
'cattr' : C,
}

def __init__(self, b, c):
print "init A()"
# must call Proxy.__init__( *delegates)
super(A,self)._ _init__(b,c)

def amethod(self,a) :
print "A::mymetho d",a
if __name__ == '__main__':
a = A(B(), C())
a.amethod('foo' )

# test bounded methods
a.bmethod('foo' )
a.bmethod2('bar ','baz')
a.cmethod('foo' )
try: a.cmethod2('bar ','baz')
except Exception, ex: print ex

# works for unbound methods too
A.bmethod(a,'fo o')
A.bmethod2(a,'b ar','baz')
A.cmethod(a, 'foo')
try: A.cmethod2(a,'b ar','baz')
except Exception, ex: print ex

# non callable attributes
print A.cattr

#====== output =============== =============== ====
init A()
A::mymethod foo
B::bmethod foo
B::bmethod2 bar baz
C::cmethod foo
'A' object has no attribute 'cmethod2'
B::bmethod foo
B::bmethod2 bar baz
C::cmethod foo
type object 'A' has no attribute 'cmethod2'
4

#======== proxies.py =============== ==========

class _ProxyMethod(ob ject):
def __init__(self, name):
self._name = name
def unbound(proxy, *args, **kwds):
method = proxy._get_targ et_attr(name)
return method(*args, **kwds)
self._unbound = unbound

def __get__(self, proxy, proxytype):
if proxy is not None:
return proxy._get_targ et_attr(self._n ame)
else:
return self._unbound
class _ProxyMeta(type ):
def __new__(meta, name, bases, namespace):
for attrname,cls in namespace.get(' DelegateMap',
{}).iteritems() :
if attrname not in namespace:
attr = getattr(cls, attrname)
if callable(attr):
namespace[attrname] = _ProxyMethod(at trname)
else:
namespace[attrname] = attr
return super(_ProxyMet a,meta).__new__ (meta, name, bases,
namespace)
class Proxy(object):
__metaclass__ = _ProxyMeta

def __init__(self, *delegates):
self._cls2deleg ate = {}
for delegate in delegates:
cls = type(delegate)
if cls in self._cls2deleg ate:
raise ValueError('Mor e than one %s delegates were
given' % cls)
self._cls2deleg ate[cls] = delegate

def _get_target_att r(self, name):
try:
cls = self.DelegateMa p[name]
delegate = self._cls2deleg ate[cls]
return getattr(delegat e, name)
except (KeyError, AttributeError) :
raise AttributeError( '%r object has no attribute %r' %
(self.__class__ .__name__, name))

HTH,
George
Sep 25 '08 #11
George Sakkis wrote:
I'm not sure if the approach below deals with all the issues, but one
thing it does is decouple completely the proxied objects from the
proxy:
<snip/>
class _ProxyMeta(type ):
<snip/>

It smelled to me more and more like metaclass too, I was just trying to
avoid them :)

Your code looks awefully close to what I'm trying to do, except it looks bit
heavier than decorators. Seems like decorators are not going to happen in
this part of project for me anyway, however the whole discussion gave me a
lot to think about. Thank you Bruno, Aaron, Diez and George.

Thanks for the concrete code with metaclass. I'm going to study it
thoroughly to see if I can spot caveats/issues for my usecases however it
seems to put me on the right track. I never used metaclasses before and
decorators seemed to be bit more straight-forward to me :) ..oh well.

Sep 25 '08 #12
Dmitry S. Makovey a écrit :
(snip)
I never used metaclasses before and
decorators seemed to be bit more straight-forward to me :) ..oh well.
Don't be afraid !-) While it's true that they can be a bit confusing at
first, metaclasses are just classes - whose instances happens to be
classes themselves.

And while you're certainly right to not jump on metaclasses *before*
hanving spent a bit time thinking of other possible solutions, there's
nothing wrong with using metaclasses when that's really what you need...
Sep 26 '08 #13
On Sep 25, 10:41*am, "Dmitry S. Makovey" <dmi...@athabas cau.cawrote:
Diez B. Roggisch wrote:
Dmitry S. Makovey schrieb:
Dmitry S. Makovey wrote:
In my real-life case A is a proxy to B, C and D instances/objects, not
just one.
forgot to mention that above would mean that I need to have more than one
decorator function like AproxyB, AproxyC and AproxyD or make Aproxy
smarter about which property of A has instance of which class etc.
__getattr__?

see, in your code you're assuming that there's only 1 property ( 'b' )
inside of A that needs proxying. In reality I have several. So in your code
self._delegate should be at least a tupple or a list. Plus what you're
doing - you just promiscuously passing any method not found in Proxy to
self._delegate which is not what I need as I need to pass only a subset of
calls, so now your code needs to acquire dictionary of "allowed" calls, and
go over all self._delegates to find if any one has it which is not
efficient since there IS a 1:1 mapping of A::method -B::method so lookups
shouldn't be necessary IMO (for performance reasons).
No, really, Diez has posted the canonical Proxy form in Python, using
__getattr__ on the proxy, and then redirecting to the contained
delegate object. This code does *not* assume that only one property
('b'? where did that come from?) is being redirected - __getattr__
will intercept all attribute lookups and redirect them to the
delegate.

If you need to get fancier and support this single-proxy-to-multiple-
delegates form, then yes, you will need some kind of map that says
which method should delegate to which object. Or, if it is just a
matter of precedence (try A, then try B, then...), then use hasattr to
see if the first delegate has the given attribute, and if not, move on
to the next.
>
I'm trying to figure out if there
is another way to use decorators for my scenario or is there another way of
achieving the same thing without using decorators and without bloating up
the code with alternative solution.

Another way could be to use Metaclass to populate class with method upon
declaration but that presents quite a bit of "special" cruft which is more
than I have to do with decorators :) (but maybe it's all *necessary* ? )
Your original question was "is decorator the right thing to use?" For
this application, the answer is "no". It sounds like you are trying
to force this particular to solution to your problem, but you are
probably better off giving __getattr__ intercepting another look.

-- Paul
Sep 26 '08 #14
Paul McGuire wrote:
>see, in your code you're assuming that there's only 1 property ( 'b' )
inside of A that needs proxying. In reality I have several.
<snip/>
>
No, really, Diez has posted the canonical Proxy form in Python, using
__getattr__ on the proxy, and then redirecting to the contained
delegate object. This code does *not* assume that only one property
('b'? where did that come from?) is being redirected - __getattr__
will intercept all attribute lookups and redirect them to the
delegate.

If you need to get fancier and support this single-proxy-to-multiple-
delegates form, then yes, you will need some kind of map that says
which method should delegate to which object. Or, if it is just a
matter of precedence (try A, then try B, then...), then use hasattr to
see if the first delegate has the given attribute, and if not, move on
to the next.
that is what I didn't like about it - I have to iterate over delegates when
I can build direct mapping once and for all and tie it to class
definition ;)
Your original question was "is decorator the right thing to use?" For
this application, the answer is "no".
yeah. seems that way. in the other fork of this thread you'll find my
conclusion which agrees with that :)
It sounds like you are trying
to force this particular to solution to your problem, but you are
probably better off giving __getattr__ intercepting another look.
__getattr__ implies constant lookups and checks (for filtering purposes) - I
want to do them once, attach generated methods as native methods and be
done with it. That is why I do not like __getattr__ in this particular
case. Otherwise - you're right.

Sep 26 '08 #15
Bruno Desthuilliers wrote:
Hem... I'm afraid you don't really take Python's dynamic nature into
account here. Do you know that even the __class__ attribute of an
instance can be rebound at runtime ? What about 'once and for all' then ?
must've been wrong wording on my part. Dynamic nature is exactly what I
wanted to use :) except that I do not expect clients to take advantage of
it while using my classes ;)
>>Your original question was "is decorator the right thing to use?" For
this application, the answer is "no".

yeah. seems that way. in the other fork of this thread you'll find my
conclusion which agrees with that :)
>>It sounds like you are trying
to force this particular to solution to your problem, but you are
probably better off giving __getattr__ intercepting another look.

__getattr__ implies constant lookups and checks (for filtering purposes)

Unless you cache the lookups results...
sure
>- I
want to do them once, attach generated methods as native methods

What is a "native method" ? You might not be aware of the fact that
method objects are usually built anew from functions on each method
call...
again, wrong wording on my part. by native I meant: make use as much as
possible of existing machinery and override default behavior only when it's
absolutely necessary. (hopefully my wording is not off this time ;) )
>and be
done with it. That is why I do not like __getattr__ in this particular
case.

There's indeed an additional penalty using __getattr__, which is that
it's only called as a last resort. Now remember that premature
optimization is the root of evil... Depending on effective use (ie : how
often a same 'proxied' method is called on a given Proxy instance, on
average), using __getattr__ to retrieve the appropriate bound method on
the delegate then adding it to the proxy instance *as an instance
attribute* might be a way better (and simpler) optimization.
I actually ended up rewriting things (loosely based on George's suggested
code) with descriptors and not using metaclasses or decorators (so much for
my desire to use them).

With following implementation (unpolished at this stage but already
functional) I can have several instances of B objects inside of A object
and proxy certain methods to one or another object (I might be having a
case when I have A.b1 and A.b2 and passing some methods to b1 and others to
b2 having both of the same class B, maybe even multiplexing). This one
seems to be fairly light as well without need to scan instances (well,
except for one getattr, but I couldn't get around it). Maybe I didn't
account for some shoot-in-the-foot scenarios but I can't come up with any.
Last time I played with __getattr__ I shot myself in the foot quite well
BTW :)

class ProxyMethod(obj ect):

def __init__(self,o b_name,meth):
self.ob_name=ob _name
self.meth=meth

def my_call(self,in stance,*argv,** kw):
ob=getattr(inst ance,self.ob_na me)
cls=self.meth.i m_class
return self.meth.__get __(ob,cls)(*arg v,**kw)

def __get__(self,in stance,owner):
if not instance:
return self.my_call
ob=getattr(inst ance,self.ob_na me)
cls=self.meth.i m_class
return self.meth.__get __(ob,cls)

class B:
def __init__(self):
self.val='bval'

def bmethod(self,a) :
print "B::bmethod ",
print a, self.val

class A:
b=None

def __init__(self,b =None):
self.val='aval'
self.b=b
b.val='aval-b'

def mymethod(self,a ):
print "A::mymetho d, ",a

bmethod=ProxyMe thod('b',B.bmet hod)

b=B()
b.bmethod('foo' )
a=A(b)
b=B()
b.val='newval'
a.mymethod('baz ')
a.bmethod('bar' )
A.bmethod(a,'zo om')

Sep 26 '08 #16
On Sep 26, 3:03*pm, Bruno Desthuilliers
<bdesth.quelque ch...@free.quel quepart.frwrote :
Dmitry S. Makovey a écrit :
Paul McGuire wrote:
>see, in your code you're assuming that there's only 1 property ( 'b' )
inside of A that needs proxying. In reality I have several.
<snip/>
No, really, Diez has posted the canonical Proxy form in Python, using
__getattr__ on the proxy, and then redirecting to the contained
delegate object. *This code does *not* assume that only one property
('b'? where did that come from?) is being redirected - __getattr__
will intercept all attribute lookups and redirect them to the
delegate.
If you need to get fancier and support this single-proxy-to-multiple-
delegates form, then yes, you will need some kind of map that says
which method should delegate to which object. *Or, if it is just a
matter of precedence (try A, then try B, then...), then use hasattr to
see if the first delegate has the given attribute, and if not, move on
to the next.
that is what I didn't like about it - I have to iterate over delegates when
I can build direct mapping once and for all and tie it to class
definition ;)

Hem... I'm afraid you don't really take Python's dynamic nature into
account here. Do you know that even the __class__ attribute of an
instance can be rebound at runtime ? What about 'once and for all' then ?

But anyway:
Your original question was "is decorator the right thing to use?" *For
this application, the answer is "no". *
yeah. seems that way. in the other fork of this thread you'll find my
conclusion which agrees with that :)
It sounds like you are trying
to force this particular to solution to your problem, but you are
probably better off giving __getattr__ intercepting another look.
__getattr__ implies constant lookups and checks (for filtering purposes)

Unless you cache the lookups results...
- I
want to do them once, attach generated methods as native methods

What is a "native method" ? You might not be aware of the fact that
method objects are usually built anew from functions on each method call....
and be
done with it. That is why I do not like __getattr__ in this particular
case.

There's indeed an additional penalty using __getattr__, which is that
it's only called as a last resort. Now remember that premature
optimization is the root of evil... Depending on effective use (ie : how
often a same 'proxied' method is called on a given Proxy instance, on
average), using __getattr__ to retrieve the appropriate bound method on
the delegate then adding it to the proxy instance *as an instance
attribute* might be a way better (and simpler) optimization.
That prohibits using a descriptor in the proxied classes, or at least
the proxied functions, since you break descriptor protocol and only
call __get__ once. Better to cache and get by name. It's only slower
by the normal amount, and technically saves space, strings vs.
instancemethod objects (except for really, really long function names).
Sep 26 '08 #17
Aaron "Castironpi " Brady wrote:
That prohibits using a descriptor in the proxied classes, or at least
the proxied functions, since you break descriptor protocol and only
call __get__ once. Better to cache and get by name. It's only slower
by the normal amount, and technically saves space, strings vs.
instancemethod objects (except for really, really long function names).
that is an interesting point since I didn't think about having descriptors
in proxied classes. my reworked code clearly breaks when descriptors are
thrown at it. It will break with methods in proxied objects that are
implemented as objects too. Now I adjusted constructor a bit to account for
that (I just can't figure out case when I'll be proxying descriptors unless
they return function but than I don't see benefit in using descriptor for
that, probably because I haven't used them much).

class ProxyMethod(obj ect):
def __init__(self,o b_name,meth):
self.ob_name=ob _name
if not hasattr(meth,'i m_class'):
if hasattr(meth,'_ _call__'):
self.meth=getat tr(meth,'__call __')
else:
raise ValueError("Met hod should be either a class method or
a callable class")
else:
self.meth=meth
Sep 26 '08 #18
On Sep 26, 6:38*pm, "Dmitry S. Makovey" <dmi...@athabas cau.cawrote:
I actually ended up rewriting things (loosely based on George's suggested
code) with descriptors and not using metaclasses or decorators (so much for
my desire to use them).

With following implementation (unpolished at this stage but already
functional) I can have several instances of B objects inside of A object
and proxy certain methods to one or another object (I might be having a
case when I have A.b1 and A.b2 and passing some methods to b1 and others to
b2 having both of the same class B, maybe even multiplexing).
You seem to enjoy pulling the rug from under our feet by changing the
requirements all the time :)
This one
seems to be fairly light as well without need to scan instances (well,
except for one getattr, but I couldn't get around it). Maybe I didn't
account for some shoot-in-the-foot scenarios but I can't come up with any..
Last time I played with __getattr__ I shot myself in the foot quite well
BTW :)

[code snipped]

class A:
* * b=None
^^^^^^ you don't need this
* * def __init__(self,b =None):
* * * * self.val='aval'
* * * * self.b=b
* * * * b.val='aval-b'

* * def mymethod(self,a ):
* * * * print "A::mymetho d, ",a

* * bmethod = ProxyMethod('b' ,B.bmethod)
Although this works, the second argument to ProxyMethod shouldn't be
necessary, it's semantically redundant; ideally you would like to
write it as "bmethod = ProxyMethod('b' )". As before, I don't think
that's doable without metaclasses (or worse, stack frame hacking).
Below is the update of my original recipe; interestingly, it's
(slightly) simpler than before:

#======= usage =============== =============== ==========

from proxies import Proxy

class B(object):
def __init__(self, val): self.val = val
def bmethod(self,n) : print "B::bmethod ", self.val, n
def bmethod2(self,n ,m): print "B::bmethod 2", self.val, n, m

class C(object):
def __init__(self, val): self.val = val
def cmethod(self,x) : print "C::cmethod ", self.val, x
def cmethod2(self,x ,y): print "C::cmethod2",s elf.val, x, y
cattr = 4

class A(Proxy):
# DelegateMap:
# Maps each delegate method to the proxy attribute that refers to
the
# respective delegate object
DelegateMap = {
'bmethod' : 'b1',
'bmethod2': 'b2',
'cmethod' : 'c',
# do NOT delegate C.cmethod2
#'cmethod2': 'c',
}

def __init__(self, b1, b2, c):
print "init A()"
# must call Proxy.__init__
super(A,self)._ _init__(b1=b1, b2=b2, c=c)

def amethod(self,a) :
print "A::mymetho d",a

if __name__ == '__main__':
a = A(B(10), B(20), C(30))
a.amethod('foo' )

print "bound proxy calls"
a.bmethod('foo' )
a.bmethod2('bar ','baz')
a.cmethod('foo' )
try: a.cmethod2('bar ','baz')
except Exception, ex: print ex

print "unbound proxy calls"
A.bmethod(a,'fo o')
A.bmethod2(a,'b ar','baz')
A.cmethod(a, 'foo')
try: A.cmethod2(a,'b ar','baz')
except Exception, ex: print ex

#======= output =============== =============== ==========

init A()
A::mymethod foo
bound proxy calls
B::bmethod 10 foo
B::bmethod2 20 bar baz
C::cmethod 30 foo
'A' object has no attribute 'cmethod2'
unbound proxy calls
B::bmethod 10 foo
B::bmethod2 20 bar baz
C::cmethod 30 foo
type object 'A' has no attribute 'cmethod2'

#====== proxies.py =============== =============== ========

class _ProxyMeta(type ):
def __new__(meta, name, bases, namespace):
for methodname in namespace.get(' DelegateMap', ()):
if methodname not in namespace:
namespace[methodname] = _ProxyMethod(me thodname)
return super(_ProxyMet a,meta).__new__ (meta, name, bases,
namespace)
class _ProxyMethod(ob ject):
def __init__(self, name):
self._name = name

def __get__(self, proxy, proxytype):
if proxy is not None:
return proxy._get_targ et_attr(self._n ame)
else:
return self._unbound_m ethod

def _unbound_method (self, proxy, *args, **kwds):
method = proxy._get_targ et_attr(self._n ame)
return method(*args, **kwds)
class Proxy(object):
__metaclass__ = _ProxyMeta

def __init__(self, **attr2delegate ):
self.__dict__.u pdate(attr2dele gate)

def _get_target_att r(self, name):
try:
delegate = getattr(self, self.DelegateMa p[name])
return getattr(delegat e, name)
except (KeyError, AttributeError) :
raise AttributeError( '%r object has no attribute %r' %
(self.__class__ .__name__, name))
HTH,
George
Sep 27 '08 #19
George Sakkis wrote:
You seem to enjoy pulling the rug from under our feet by changing the
requirements all the time :)
but that's half the fun! ;)

Bit more seriously - I didn't know I had those requirements until now :) I'm
kind of exploring where can I get with those ideas. Initial post was based
exactly on what I had in my code with desire to make it more
automated/{typo,fool}-proof/robust, elegant and possibly faster. Hopefully
such behavior is not off-topic on this list (if it is - let me know and
I'll go exploring solo).
Although this works, the second argument to ProxyMethod shouldn't be
necessary, it's semantically redundant; ideally you would like to
write it as "bmethod = ProxyMethod('b' )".
since I'm already on exploratory trail (what about that rug being pulled
from under....?) With my code I can do really dirty tricks like this (not
necessary that I'm going to):

class B_base:
def bmethod(self):
print 'B_base'

class B(B_base):
def bmethod(self):
print 'B'

class A:
bmethod=ProxyMe thod('b',B_base .bmethod)
As before, I don't think
that's doable without metaclasses (or worse, stack frame hacking).
Below is the update of my original recipe; interestingly, it's
(slightly) simpler than before:
Out of curiosity (and trying to understand): why do you insist on
dictionaries with strings contents ( {'bmethod' : 'b1' } ) rather than
something more explicit ? Again, I can see that your code is working and I
can even understand what it's doing, just trying to explore alternatives :)

I guess my bias is towards more explicit declarations thus

bmethod=ProxyMe thod('b',B.bmet hod)

looks more attractive to me, but I stand to be corrected/educated why is
that not the right thing to do?

Another thing that turns me away from string dictionaries is that those are
the ones causing me more trouble hunting down typos. Maybe it's just "my
thing" so I'm not going to insist on it. I'm open to arguments against that
theory.

One argument I can bring in defence of more explicit declarations is IDE
parsing when autocompletion for B.bme... pops up (suggesting bmethod and
bmethod2) and with 'b':'bmethod' it never happens. However that's not good
enough reason to stick to it if it introduces other problems. What kind of
problems could those be?

P.S.
so far I find this discussion quite educating BTW. I am more of a "weekend
programmer" with Python - using basic language for most of things I need,
however I decided to explore further to educate myself and to write more
efficient code.
Sep 27 '08 #20

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

Similar topics

14
2320
by: Sandy Norton | last post by:
If we are going to be stuck with @decorators for 2.4, then how about using blocks and indentation to elminate repetition and increase readability: Example 1 --------- class Klass: def __init__(self, name):
24
2074
by: Steven Bethard | last post by:
I think one of the biggest reasons we're having such problems coming to any agreement on decorator syntax is that each proposal makes a number of syntax decisions, not just one. For decorators, I see the following decisions that must be made: 1) Indicator Proposals differ on how some sort of indicator of "decoratorhood" is use. These include: * none (e.g. just the list, as in the "list-after-def" suggestion) * the '@' character
11
1639
by: Ville Vainio | last post by:
It might just be that @decorator might not be all that bad. When you look at code that uses it it's not that ugly after all. A lot of the furor about this is probably because it happened so quicly. The situation might have been different if we had seen a pronouncement a week before, in the vein of "I have chosen this syntax - it will go in to the next alpha". My chief worry was throwing away one of the few unused ascii symbols, but if...
37
2616
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 much (except for people whose tools depend on '@' ;-)). I.e., (not having the source or time to delve) the apparent semantics of the above is something roughly like
30
2514
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 closer look at function def's and call's, which is something I tend to take for granted. I'm not sure this is 100%, or if there are other ways to view it, but it seems to make sense when viewed this way. Is there a way to do this same thing...
22
2243
by: Ron_Adam | last post by:
Hi, Thanks again for all the helping me understand the details of decorators. I put together a class to create decorators that could make them a lot easier to use. It still has a few glitches in it that needs to be addressed. (1) The test for the 'function' object needs to not test for a string but an object type instead.
6
1404
by: Michele Simionato | last post by:
could ildg wrote: > I think decorator is a function which return a function, is this right? > e.g. The decorator below if from http://www.python.org/peps/pep-0318.html#id1. > > def accepts(*types): > def check_accepts(f): > assert len(types) == f.func_code.co_argcount
11
1834
by: glen.coates.bigworld | last post by:
I'm developing a library at the moment that involves many classes, some of which have "exposed" capabilities. I'm trying to design a nice interface for both exposing those capabilities, and inspecting instances to find out what capabilities they have. At the moment, I'm leaning towards a superclass (Exposed) that defines a static method which is a decorator (expose) such that any derived class can mark a method with @Exposed.expose and...
4
2474
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 has all of its decorator implementation inside a decorator() method. Every other attribute access is being proxied to decorator().getParent(). Here's my attempt: -------------------------------------------------------
0
9605
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10647
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10386
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10398
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10133
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9204
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4339
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3017
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.