mixin helper class for unknown attribute access?
Steven D'Aprano
steve at REMOVETHIScyber.com.au
Mon Oct 31 07:40:24 EST 2005
On Mon, 31 Oct 2005 10:39:40 +0000, Alex Hunsley wrote:
> I know that I can catch access to unknown attributes with code something
> like the following:
>
> class example:
> def __getattr__(self, name):
> if name == 'age':
> return __age
> else:
> raise AttributeError
>
>
> but is there an existing mixin helper class in Python (or one someone
> has written) already that will assist with this? (Just not wanting to
> reinvent the wheel....)
Too late.
py> class Example:
... age = 0
...
py> Example.age
0
py> Example.aeg
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: class Example has no attribute 'aeg'
It works for instances too:
py> Example().aeg
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: Example instance has no attribute 'aeg'
Your __getattr__ code is completely unnecessary.
--
Steven.
More information about the Python-list
mailing list