how do i create such a thing?
Pedro Werneck
pedro.werneck at terra.com.br
Sun Jan 30 17:31:57 EST 2005
Hi,
If you need direct access to some atribute, use object.__getattribute__.
>>> class DefaultAttr(object):
... def __init__(self, default):
... self.default = default
... def __getattribute__(self, name):
... try:
... value = object.__getattribute__(self, name)
... except AttributeError:
... value = self.default
... return value
...
>>> x = DefaultAttr(99)
>>>
>>> print x.a
99
>>> x.a = 10
>>> print x.a
10
>>>
On Sun, 30 Jan 2005 13:54:38 -0800
Lowell Kirsh <lkirsh at cs.ubc.ca> wrote:
> I want to create a class called DefaultAttr which returns a default
> value for all attributes which haven't been given values yet. It will
> work like this:
>
> >> x = DefaultAttr(99)
> >> print x.foo
> 99
> >> print x.bar
> 99
> >> x.foo = 7
> >> print x.foo
> 7
>
> I already have a similar class called DefaultDict which works
> similarly which I assume would be a good thing to use.
>
> Lowell
> --
> http://mail.python.org/mailman/listinfo/python-list
More information about the Python-list
mailing list