K
kedra marbun
kedra marbun a écrit :
Nothing prevents you to pass a "name" to the descriptor instance when
instanciating it, ie:
class Desc(object):
def __init__(self, name):
self.name = name
def __get__(self, inst, cls):
# ...
def __set__(self, inst, value):
# ...
class Foo(object):
bar = Desc("bar")
baaz = Desc("baaz")
Ok, this is not necessarily what you were looking for, but it's IMHO
less brittle than relying on which attribute name was looked up (which
is something the descriptor shouldn't have to care about).
In Python 2.x, not that I know (but it may have passed under my radar).
If what you want it to automate delegation of a set of methods without
too much manual coding, you can use a custom metaclass that will add the
relevant methods to the class, based on (ie) a list (or mapping) of
methods names. But that might be a bit overkill.
I don't see the point of using delegation on a class attribute. That's
typically what inheritance is for.
ah, our friend mixin, it's just a sample though
Err... Did you try the simple way ?
return getattr(self.a, name)
argh, that should be
class A:
def do_this(self, ins): ...