__getattr__ and static vs. instantiated

tvaughan thomas.david.vaughan at gmail.com
Tue Sep 11 19:50:07 EDT 2007


Hi,

Let's say I have:

class Persistable(object):

        __attrs__ = {}

        def __getattr__(self, name):
                if name in self.__attrs__:
                        return self.__attrs__[name]['value']
                else:
                        return Object.__getattr__(self, name)

        def __setattr__(self, name, value):
                if name in self.__attrs__:
                        self.__attrs__[name]['value'] = value
                else:
                        Object.__setattr__(self, name, value)

And the I have:

class Person(Persistable):

        __storm_table__ = 'person'

        id = Int(primary=True)

        __attrs__ = {
                'name': {
                        'lang': 'en',
                        'value': Unicode(),
                },
        }

        def __init__(self):
                self.id = int(random.random() * 1000)

I can do this:

person = Person()
person.name = 'Jane Smith'

But I cannot do:

Person.name = 'Jane Smith'

or use Person.name in a Storm query like:

Person.name == 'Jane Smith'

__getattr__ is only called when using an instantiated class, and
never, it seems, in a static case. Why? How do I work around this?
Thanks.

-Tom

P.S. This is Python 2.5 on Ubuntu Feisty x86.




More information about the Python-list mailing list