Module Structure/Import Design Problem

Arnaud Delobelle arnodel at googlemail.com
Tue Nov 25 15:44:17 EST 2008


Bruno Desthuilliers <bdesth.quelquechose at free.quelquepart.fr> writes:

> Rafe a écrit :
>> On Nov 20, 2:06 pm, Arnaud Delobelle <arno... at googlemail.com> wrote:
> (snip)
>>>
>>> Or (better IMHO) you can make types register themselves with the factory
>>> function (in which case it would have some state so it would make more
>>> sense to make it a factory object).
>>>
>>
>> Can you elaborate on what you mean by 'register' with the factory
>> function?
>
> I think Arnaud means something like:
>
> # baselib.py
> class Base(object):
>     _registry = {}
>
>     @classmethod
>     def register(cls, subclass):
>         cls._registry[subclass.__name__] = subclass
>
>     @classmethod
>     def get_class(cls, classname):
>         return cls._registry[classname]
>
>
> # typelib.py
> from baselib import Base
>
> class Sub(Base):
>    pass
>
> Base.register(Sub)

That's what I meant.  Sorry Rafe I missed your follow-up.

-- 
Arnaud



More information about the Python-list mailing list