__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 -- http://mail.python.org/mailman/listinfo/python-list
Re: __getattr__ on non-instantiated class
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 :-\ -- http://mail.python.org/mailman/listinfo/python-list
Re: __getattr__ on non-instantiated class
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 -- http://mail.python.org/mailman/listinfo/python-list
