An object that creates (nested) attributes automatically on assignment

Edd edd at nunswithguns.net
Sat Apr 11 14:37:41 CEST 2009


On Apr 11, 12:54 pm, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:

> Ah, now it is more clear.
>
> Okay, let's try this:
>
> >>> class C(object):
>
> ...     def __getattr__(self, name):
> ...             # Only called if self.name doesn't exist.
> ...             inst = self.__class__()
> ...             setattr(self, name, inst)
> ...             return inst

Ha! Perfect! I knew it should be simpler. Thanks very much!

Kind regards,

Edd



More information about the Python-list mailing list