how do i create such a thing?

L

Lowell Kirsh

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:
7

I already have a similar class called DefaultDict which works similarly
which I assume would be a good thing to use.

Lowell
 
P

Pedro Werneck

Hi,

If you need direct access to some atribute, use object.__getattribute__.

.... def __init__(self, default):
.... self.default = default
.... def __getattribute__(self, name):
.... try:
.... value = object.__getattribute__(self, name)
.... except AttributeError:
.... value = self.default
.... return value
....
 
S

Steven Bethard

Pedro said:
If you need direct access to some atribute, use object.__getattribute__.


... def __init__(self, default):
... self.default = default
... def __getattribute__(self, name):
... try:
... value = object.__getattribute__(self, name)
... except AttributeError:
... value = self.default
... return value
...

10

Of if you only want to deal with the case where the attribute doesn't
exist, you can use getattr, which gets called when the attribute can't
be found anywhere else:

py> class DefaultAttr(object):
.... def __init__(self, default):
.... self.default = default
.... def __getattr__(self, name):
.... return self.default
....
py> x = DefaultAttr(99)
py> x.a
99
py> x.a = 10
py> x.a
10

Steve
 
A

Alex Martelli

Steven Bethard said:
Of if you only want to deal with the case where the attribute doesn't
exist, you can use getattr, which gets called when the attribute can't
be found anywhere else:

py> class DefaultAttr(object):
... def __init__(self, default):
... self.default = default
... def __getattr__(self, name):
... return self.default
...
py> x = DefaultAttr(99)
py> x.a
99
py> x.a = 10
py> x.a
10

This is a good approach, but it's fragile regarding specialnames.
.... def __init__(self, default): self.default = default
.... def __getattr__(self, name): return self.default
.... Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/local/lib/python2.4/copy.py", line 87, in copy
rv = reductor(2)
TypeError: 'int' object is not callable

There are many more, worse example for classic classes, but even in
newstyle classes you must consider that once in a while a system routine
will try a getattr(someinst, '__aspecialname__', None) or the like --
and your class is making its instances claim to have ANY special name
that may be introspected for in this way. This can be a pernicious lie,
since your class in fact has no idea whatsoever what that specialname
and the corresponding value might be for.

It's HIGHLY advisable to have your __getattr__ methods raise
AttributeError for any requested name that starts and ends with double
underscores, possibly with some specific and specifically designed
exceptions.


Alex
 
A

Alex Martelli

Lowell Kirsh said:
What might these exceptions be?

For example, delegation of such requests to some other object:
def __getattr__(self, name):
return getattr(self._delegate, name)

In such cases you may decide you do not need to block __specials__,
because you're OK with having self._delegate supply them or be
responsible to raise AttributeError if necessary.


Alex
 
L

Lowell Kirsh

I'm not sure I get it. What's the purpose of using a delegate rather
than having the object itself supply the return value?
 
S

Steve Holden

Lowell said:
I'm not sure I get it. What's the purpose of using a delegate rather
than having the object itself supply the return value?

The point is that you can keep a reference to some object, and it's the
next best thing to having subclassed the object's class but with closer
control.

regards
Steve

A: Top-posting
Q: What puts things in the wrong order on newsgroup postings
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,219
Messages
2,571,117
Members
47,729
Latest member
taulaju99

Latest Threads

Top