__getattr__ and static vs. instantiated

T

tvaughan

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.
 
B

Bruno Desthuilliers

tvaughan a écrit :
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'

yes, you can. But it won't do the same thing.
or use Person.name in a Storm query like:

Person.name == 'Jane Smith'

It will obviously raise an AttributeError if the class Person doesn't
have a 'name' attribute.
__getattr__ is only called when using an instantiated class,

A class's '__getattr__' method is only triggered when looking up
attributes on an instance of the class, yes.
and
never, it seems, in a static case. Why? How do I work around this?

Since classes are instances of their type, the obvious solution would be
to define a custom metaclass implementing __getattr__. I didn't try, but
it might work.

Now is this the best thing to do is another question. You seems not to
be aware of the descriptor protocol - which is used to implement methods
and properties. This lets you define 'smart' attributes. You'll find
relevant documentation here:
http://docs.python.org/ref/descriptors.html
and here:
http://users.rcn.com/python/download/Descriptor.htm


Also, you seem to be trying to roll your own ORM. There are already
quite a few existing ones - SQLAlchemy being probably the best one
already, and also a good base (no pun) for building your own solutions.

HTH
 

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
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top