468,291 Members | 1,429 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,291 developers. It's quick & easy.

__getattribute__ and __slots__

Hi,
I try to define a (new-style) class who:
- have a __slots__ defined to be strict attributes,
- return None if the attribute is 'ok' but not set, or raise a 'normal'
error if the attribute isn't in __slots__.

This code runs, but is it the good way?

Thanks.

class test(object):
__slots__ = ['id']
def __getattr__(self, attr):
if not attr in self.__slots__: raise AttributeError
try:
return self.attr
except:
return None

Apr 14 '06 #1
1 2194
pa***********@free.fr wrote:

Python has both __getattribute__() and __getattr__(). While
__getattribute__() will always be called if you ask for an attribute
__getattr__() serves only as fallback if the attribute if not found by
other means.
I try to define a (new-style) class who:
- have a __slots__ defined to be strict attributes,
__slots__ is a performance/memory optimization. Using __slots__ to introduce
bondage & discipline through the backdoor is against Python's spirit.
- return None if the attribute is 'ok' but not set, or raise a 'normal'
error if the attribute isn't in __slots__.
The approach you have chosen is a very ineffective one. Why don't you just
set the attribute to a default value in the initializer?
This code runs, but is it the good way?
I don't think so...
class test(object):
__slots__ = ['id']
def __getattr__(self, attr):
if not attr in self.__slots__: raise AttributeError
try:
return self.attr
The line above does not do what you think it does. It just calls
test_instance.__getattr__("attr"), and since "attr" is not in __slots__ it
raises an AttributeError.
except:
return None


You should get the same effect with
class T(object): .... __slots__ = ["id"]
.... def __getattr__(self, name):
.... if name not in self.__slots__:
.... raise AttributeError
.... return 42 # for better visibility
.... t = T()
t.id 42 t.whatever Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 5, in __getattr__
AttributeError t.id = "something else"
t.id 'something else' t.whatever = "something else"

Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'T' object has no attribute 'whatever'

Peter

Apr 17 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by flori | last post: by
3 posts views Thread by Nick Jacobson | last post: by
2 posts views Thread by Ewald R. de Wit | last post: by
27 posts views Thread by Licheng Fang | last post: by
6 posts views Thread by Adam Donahue | last post: by
8 posts views Thread by bukzor | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.