Examples of descriptors?

Michele Simionato mis6 at pitt.edu
Mon Jun 2 14:39:15 EDT 2003


"Raymond Hettinger" <vze4rx4y at verizon.net> wrote in message news:<vonCa.30145$ca5.3622 at nwrdny02.gnilink.net>...
> "Edward C. Jones"
> > What, exactly, is a "descriptor"? The closest thing to a definition that
> > I can find is: a class that defines __get__, __set__, and __delete__.
> > Where can I find some examples?
> 
> Here is a draft write-up on descriptors:
>     http://tinyurl.com/d63d
> 
> Parts to be added:
> * data descriptors
> * more detail behind object.__getattribute__
> 
> Comments are welcome.
> 
> 
> Raymond Hettinger

Great job! 

I wish it was available six months ago, it would have saved me a lot
of
time! :-| 

I have a question about the difference between object.__setattr__ 
and type.__setattr__, which probably is related to the difference 
bewteen object.__getattribute__ and type.__getattribute__.

Before P23b1 I could write

>>> class M(type): pass
>>> object.__setattr__(M,'x',1)
>>> M.x
1

However, I discovered few weeks ago that under 2.3b

>>> class M(type): pass
>>> object.__setattr__(M,'x',1)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: can't apply this __setattr__ to type object

(this broke one program of fine; BTW, I haven't seen this documented
in http://www.python.org/2.3/highlights.html)

The problem goes away if I use type.__setattr__:

>>> type.__setattr__(M,'x',1)
>>> M.x
1

My question is: what's the rationale for the change ? It is simply
a performance hack or there was some subtle bug with the previous
model ?

Just curious,

                                                Michele




More information about the Python-list mailing list