__getattribute__ and __slots__

pascal.parent at free.fr pascal.parent at free.fr
Fri Apr 14 05:15:14 EDT 2006


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




More information about the Python-list mailing list