Postpone creation of attributes until needed
Marc 'BlackJack' Rintsch
bj_666 at gmx.net
Mon Jun 11 07:35:08 EDT 2007
In <1181559496.911368.219210 at p77g2000hsh.googlegroups.com>, Frank Millman
wrote:
> On Jun 11, 12:21 pm, Steven D'Aprano
> <s... at REMOVE.THIS.cybersource.com.au> wrote:
>> > I use __slots__ to catch any invalid attributes, otherwise I would get
>> > a 'maximum recursion depth exceeded' error.
>>
>> That's the wrong solution to that problem. To avoid that problem,
>> __getattr__ should write directly to self.__dict__.
>>
>
> Are you saying that instead of
>
> self.z = self.x * self.y
> return getattr(self.name)
>
> I should have
>
> self.__dict__['z'] = self.x * self.y
> return self.__dict__[name]
>
> I tried that, but I get AttributeError: 'A' object has no attribute
> '__dict__'.
That's because you used `__slots__`. One of the drawbacks of `__slots__`.
Ciao,
Marc 'BlackJack' Rintsch
More information about the Python-list
mailing list