delegation pattern via descriptor
Bruno Desthuilliers
bruno.42.desthuilliers at websiteburo.invalid
Mon Jul 5 04:42:13 EDT 2010
kedra marbun a écrit :
> i'm confused which part that doesn't make sense?
> this is my 2nd attempt to py, the 1st was on april this year, it was
> just a month, i'm afraid i haven't got the fundamentals right yet. so
> i'm gonna lay out how i got to this conclusion, CMIIW
>
> **explanation of feeling (0) on my 1st post**
> to me, descriptor is a particular kind of delegation, it takes the job
> of coding the delegation by having a contract with programmers that
> the tree meta operations (get, set, del) on attr are delegated to the
> obj that is bound to the attr
> are we agree that descriptor is a kind of delegation?
>
> the mechanism that makes descriptor works is in __getattribute__,
> __setattr__, __delattr__ of 'object' & 'type'
>
> now, if i want a single descriptor obj to be delegated to multiple
> tasks, i can't do it since __get__ doesn't get info that can be used
> to determine which task to do
> i must have diff descriptor obj for each task
>
> class Helper:
> def __init__(self, name):
> self.name = name
> def __get__(self, ins, cls):
> if self.name == 'task0': ...
> elif self.name == 'task1': ...
> else: ...
Replacing such "big switch" code with polymorphic dispatch is one of the
goals (and feature) of OO. This should be:
class Task0(object):
def __get__(self, obj, cls):
# code here
class Task1(object):
def __get__(self, obj, cls):
# code here
class A(object):
task0 = Task0()
task1 = Task1()
If you have common code to share between TaskO and Task1 then factor it
out into a base class.
> if __get__ receives the name, then i could do
>
> class Helper:
> def __get__(self, ins, cls, name):
> ...
>
> class a:
> task0 = task1 = Helper()
Yuck.
More information about the Python-list
mailing list