Add Properties to Instances?

Martin Miller ggrp1.20.martineau at dfgh.net
Mon Mar 14 16:07:29 EST 2005


Bengt Richter wrote, in part:
> On 14 Mar 2005 01:19:23 -0800, "Martin Miller"
<ggrp1.20.martineau at dfgh.net>
> wrote, in part:
> >What still puzzles me, though, is why all the above to make
properties
> >work on instances is necessary in the first place. It's certainly
not
> >clear (to me) from what is said in the How-to at:
> >>
>
>http://users.rcn.com/python/download/Descriptor.htm#invoking-descriptors
> >I suspect that it may be simply a performance issue, in other words,
it
> >was considered too slow to check for instance property/discriptors
--
> >although *why* is not clear to me.
>
> I suspect the desired semantics re precedence have evolved to make
normal
> programs easily implementable, and that probably drove the
implementation:
> """
> For objects, the machinery is in object.__getattribute__ which
transforms
> b.x into type(b).__dict__['x'].__get__(b, type(b)).
> The implementation works through a precedence chain that gives (my
added
> [1,2,3])
>
> [1] data descriptors priority over instance variables,
> [2] instance variables priority over non-data descriptors,
> [3] and assigns lowest priority to __getattr__ if provided.
>
> The full C implementation can be found in PyObject_GenericGetAttr()
in
> Objects/object.c.
> """

I haven't examined the C code in Objects/object.c to see *how* the
semantics are implemented because that's not really the point...which
is the descriptions of what's suppose to happen don't seem to match
what actually does. To illustrate, consider:
> class Foobar(object):
>     pass
>
> def myget(self, obj, type=None):
>     return 42
>
> def myset(self, value):
>     raise AttributeError("this is a read-only property")
>
> foobar = Foobar()
> foobar.x = property(myget, myset)
>
> print "foobar.x:", foobar.x

Which prints:
> foobar.x: <property object at 0x00AE5850>

Ignoring "why" issue, my question becomes:

foobar.x is a data descriptor property, however object.__getattribute__
does *not* seem to be treating it as such and handling it the way
described either by you or in what is written in the how-to.
Specifically the statements that:
> For objects, the machinery is in object.__getattribute__ which
transforms
> b.x into type(b).__dict__['x'].__get__(b, type(b)).

This doesn't seem to be occuring. Am I missing something?

Thanks,
Martin




More information about the Python-list mailing list