Descriptor puzzlement

J

John Roth

Using Python 2.2.3, I create this script:

[beginning of script]

class AnObject(object):
"This might be a descriptor, but so far it doesn't seem like it"
def __init__(self, somedata):
self.it = somedata
def __get__(self, obj, type=None):
print "in get method"
return self.it
def __set__(self, obj, it):
print "in set method"
self.somedata = it
return None
## def foo(self):
## return 1

class AnotherObject(object):
def __init__(self):
self.prop = AnObject("snafu")

myClass = AnotherObject()
print myClass.prop
myClass.prop = "foobar"
print myClass.prop

[end of script]

Then I execute it:

C:\Documents and Settings\John\My Documents\Projects\AstroPy4>b

C:\Documents and Settings\John\My Documents\Projects\AstroPy4>python b.py
<__main__.AnObject object at 0x0086D248>
foobar

It doesn't look like the descriptor protocol is getting
invoked at all.

What's happening here?

John Roth
 
J

Jacek Generowicz

John Roth said:
It doesn't look like the descriptor protocol is getting
invoked at all.

What's happening here?

Try changing the following
class AnotherObject(object):
def __init__(self):
self.prop = AnObject("snafu")

to

class AnotherObject(object):
prop = AnObject("snafu")
 
M

Michael Hudson

[snippety]
It doesn't look like the descriptor protocol is getting
invoked at all.

What's happening here?

Descriptors need to be attached to classes.

Cheers,
mwh
 
M

Mirko Zeibig

John Roth said the following on 01/08/2004 01:34 PM:
Using Python 2.2.3, I create this script:

[beginning of script]

class AnObject(object):
"This might be a descriptor, but so far it doesn't seem like it"
def __init__(self, somedata):
self.it = somedata
def __get__(self, obj, type=None):
print "in get method"
return self.it
def __set__(self, obj, it):
print "in set method"
self.somedata = it
return None
## def foo(self):
## return 1
Hm, I don't know __set__ and __get__, there are __getattr__ (or
__getattribute__) and __setattr__ for dynamically assigning attributes.
Or take a look at properties
(http://www.python.org/2.2/descrintro.html#property)
class AnotherObject(object):
def __init__(self):
self.prop = AnObject("snafu")

myClass = AnotherObject()
print myClass.prop
Now just do:
print id(myClass.prop) to see the internal reference.
myClass.prop = "foobar"
Here you bind an immutable string-object to myClass.prop, the object of
type AnObject you have bound before has no further references in the
code and will be garbage collected.

If you create a destructor for AnObject:

def __del__(self):
print "%s.__del__" % self

you will see that this happens immediately.
print myClass.prop

Regards
Mirko
 
P

Peter Otten

John said:
Using Python 2.2.3, I create this script:

[beginning of script]

class AnObject(object):
"This might be a descriptor, but so far it doesn't seem like it"
def __init__(self, somedata):
self.it = somedata
def __get__(self, obj, type=None):
print "in get method"
return self.it
def __set__(self, obj, it):
print "in set method"
self.somedata = it
return None
## def foo(self):
## return 1

class AnotherObject(object):
def __init__(self):
self.prop = AnObject("snafu")

myClass = AnotherObject()
print myClass.prop
myClass.prop = "foobar"
print myClass.prop

[end of script]

Then I execute it:

C:\Documents and Settings\John\My Documents\Projects\AstroPy4>b

C:\Documents and Settings\John\My Documents\Projects\AstroPy4>python b.py
<__main__.AnObject object at 0x0086D248>
foobar

It doesn't look like the descriptor protocol is getting
invoked at all.

What's happening here?

The descriptor protocol works on the class, not the instance, so

class AnotherObject(object):
prop = AnObject("snafu")

or something similar should work. This means in particular that you have to
store the property's state in the AnotherObject rather than the AnObject
instance.

Peter
 
F

Francis Avila

Mirko Zeibig wrote in message ...
John Roth said the following on 01/08/2004 01:34 PM:
Hm, I don't know __set__ and __get__, there are __getattr__ (or
__getattribute__) and __setattr__ for dynamically assigning attributes.
Or take a look at properties
(http://www.python.org/2.2/descrintro.html#property)

Properties are just a wrapper/interface/application of/to descriptors (whose
protocol involves __set__ and __get__).
http://users.rcn.com/python/download/Descriptor.htm for details.
 
T

Terry Reedy

John Roth said:
Using Python 2.2.3, I create this script:

[beginning of script]

class AnObject(object):
"This might be a descriptor, but so far it doesn't seem like it"
def __init__(self, somedata):
self.it = somedata
def __get__(self, obj, type=None):
print "in get method"
return self.it
def __set__(self, obj, it):
print "in set method"
self.somedata = it

Did you mean to set self.it to match the __init__ and __get__ methods?
Or am I missing something about the esoterics of properties?

Terry J. Reedy
 
J

John Roth

Terry Reedy said:
John Roth said:
Using Python 2.2.3, I create this script:

[beginning of script]

class AnObject(object):
"This might be a descriptor, but so far it doesn't seem like it"
def __init__(self, somedata):
self.it = somedata
def __get__(self, obj, type=None):
print "in get method"
return self.it
def __set__(self, obj, it):
print "in set method"
self.somedata = it

Did you mean to set self.it to match the __init__ and __get__ methods?
Or am I missing something about the esoterics of properties?

No, you're not missing anything. This was simply the result
of futzing around trying to get it to work, and has no other
conceptual value at all. The problem turned out to be that
I was putting it in the instance instead of the class. [sigh.]

John Roth
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top