469,306 Members | 2,512 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

__getattr__ on non-instantiated class

Hi
I was wondering if it is possible to have the various magic methods,
mainly __getattr__ and __setattr__, and @property attributes called
when accessing the attribute of a non-intantiated class.

Imagin something like this:
#####
class MyClass:
@property
def prop(self):
print "Accessed"
return "ABCD"

print MyClass.prop
#####
having it printing:
#####
Accessed
ABCD
#####

Thanks very much

May 3 '06 #1
5 1644
Fredp wrote:
Hi
I was wondering if it is possible to have the various magic methods,
mainly __getattr__ and __setattr__, and @property attributes called
when accessing the attribute of a non-intantiated class.

Imagin something like this:
#####
class MyClass:
@property
def prop(self):
print "Accessed"
return "ABCD"

print MyClass.prop
#####
having it printing:
#####
Accessed
ABCD
#####

Thanks very much

Looks like you want Python to execute a method on an uninstantiated
class. I can't imagine how you would use such a thing. Can you
give us a real-life "use case"?

This produces the output you want:

m=MyClass()
print m.prop()

-Larry Bates
May 3 '06 #2

Larry Bates ha scritto:
Fredp wrote:
Hi
I was wondering if it is possible to have the various magic methods,
mainly __getattr__ and __setattr__, and @property attributes called
when accessing the attribute of a non-intantiated class.

Imagin something like this:
#####
class MyClass:
@property
def prop(self):
print "Accessed"
return "ABCD"

print MyClass.prop
#####
having it printing:
#####
Accessed
ABCD
#####

Thanks very much

Looks like you want Python to execute a method on an uninstantiated
class. I can't imagine how you would use such a thing. Can you
give us a real-life "use case"?

This produces the output you want:

m=MyClass()
print m.prop()

-Larry Bates

I have something like a simple ORM which objects haven't a fixed number
of fields, and I need to have properties (or methods) for each of them,
but currently it is more comfortable for me to use uninstantiaded
classes (as someway SQLObject does).
I guess I'd better taking another approach to him, maybe using
something from ASPN cookbook :-\

May 3 '06 #3
Larry Bates wrote:
Fredp wrote:
Hi
I was wondering if it is possible to have the various magic methods,
mainly __getattr__ and __setattr__, and @property attributes called
when accessing the attribute of a non-intantiated class.

Imagin something like this:
#####
class MyClass:
@property
def prop(self):
print "Accessed"
return "ABCD"

print MyClass.prop
#####
having it printing:
#####
Accessed
ABCD
#####

Thanks very much

Looks like you want Python to execute a method on an uninstantiated
class.


s/uninstantiated//

Python's classes are objects, and as such can have attributes and
methods (read about staticmethod or classmethod).
I can't imagine how you would use such a thing.
I do - that's something I do quite frequently.

This produces the output you want:

m=MyClass()
print m.prop()


But this is not what the OP asked for !-)
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
May 3 '06 #4
Fredp wrote:
(snip)
I have something like a simple ORM which objects haven't a fixed number
of fields, and I need to have properties (or methods) for each of them,
dumbiest possible example, but this should het you started

class Field(object):
# dummy
def __init__(self, **kw):
self.__dict__.update(kw)

class SomeORMObject(object):
# this is a class property
fields = {'toto' : Field(type='int', primary=True),
'tata' : Field(type='varchar', maxlen=255, default=''),
}

# this is a class method,
# the first param is the class object
@classmethod
def get_field(cls, fldname):
return cls.fields.get(fldname, None)
SomeORMObject.get_field('toto')

Properties won't probably cut it, but you can write custom descriptors
(google or search python.org for a description of what descriptors are -
for the record, properties are a kind of descriptor). I did use this
kind of stuff for an 'object/ldap mapper', using descriptors for ldap
attributes and some classmethod for building queries etc...
but currently it is more comfortable for me to use uninstantiaded
classes (as someway SQLObject does).
I guess I'd better taking another approach to him, maybe using
something from ASPN cookbook :-\


Your approach is quite sensible IMHO - minus one detail : you should
avoid reinventing the Square Wheel(tm). There already are some Python
orms. I'm not found of SQLObject, but I've played a bit with SQLAlchemy
and it seems quite promising.

HTH
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
May 3 '06 #5
Well, actually I need only a tiny subset of a ORM. Or perhaps what I
need isn't exactly an ORM, because I have only one kind of Object with
a various number of Fields on a separate Table, so I was looking to
create something that could fit this scheme and nothing more, but I see
in any case it needs much work.
SQLObject is a bit unflexible for me (well, I didn't go too deep in
it), so I'll give a look to SqlAlchemy, that seems very interesting -
though I don't like its "look'n'feel" very much.

Thank you very much

-Federico Pelloni

May 3 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Greg Brunet | last post: by
3 posts views Thread by Chris Curvey | last post: by
1 post views Thread by limodou | last post: by
3 posts views Thread by Thomas Heller | last post: by
2 posts views Thread by Harold Fellermann | last post: by
7 posts views Thread by bearophileHUGS | last post: by
2 posts views Thread by Stef Mientki | last post: by
7 posts views Thread by =?UTF-8?Q?Alexandru_Mo=C8=99oi?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.