FEEDBACK WANTED: Type/class unification

Guido van Rossum guido at python.org
Tue Aug 14 10:45:41 EDT 2001


Robin Becker <robin at jessikat.fsnet.co.uk> writes:
> >> I've just read http://www.python.org/2.2/descrintro.html :
> ...
> I've been reading this as well and some of the peps that go into it.
> 
> I don't seem to be able to understand all the get/set attribute stuff. 
> 
> Is the thing returned by getset only applicable to instance attributes. 
> 
> It appears on the face of it to be an object which traps lhs and rhs
> reference. If that is the case would it not be better to provide
> mechanisms to trap these references on all kinds of objects.
> 
> I guess what I'm trying to get at is can getset be used to create a
> readonly module level global or can it only be used for attributes.

The getset descriptor works in cooperation with the default
__getattr__ implementation.  When a.foo is requested, __getattr__
looks up a.__class__.foo first; if this is found and it is a
descriptor, the descriptor's __get__ is called to finish the
__getattr__ request.  Ditto for __setattr__.

If you could subclass modules (you can't yet in 2.2a1, but you will in
2.2), you could do this for module attribute references, but global
variable references don't go through __getattr__ and __setattr__ on
the module -- they use the C APIs PyDict_GetItem and PyDict_SetItem
and you can't override those.

--Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-list mailing list