(newbie) Is there a way to prevent "name redundancy" in OOP ?

Michele Simionato michele.simionato at gmail.com
Wed Jan 10 23:47:14 EST 2007


Martin v. Löwis wrote:
> If you want to, you can combine this with the factory/singleton
> patterns, to transparently create the objects on first access:
>
> class Registry:
>   def __getitem__(self, name):
>     # only invoked when attribute is not set
>     r = pin(name)
>     setattr(self, name, r)
>     return r
> registry = Registry()
>
> class pin:
>   def __init__(self, name):
>     self.name = name
>
> print registry.aap

<nitpick>
You probably meant __getattr__ instead of __getitem__  here ;)
</niptick>

This is an interesting approach, but I would probabily use the
traditional registry you discussed
before (too much magic here).

        Michele Simionato




More information about the Python-list mailing list