Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Descriptor puzzlement

Reply
Thread Tools

Descriptor puzzlement

 
 
John Roth
Guest
Posts: n/a
 
      01-08-2004
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


 
Reply With Quote
 
 
 
 
Jacek Generowicz
Guest
Posts: n/a
 
      01-08-2004
"John Roth" <(E-Mail Removed)> writes:

> 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")

 
Reply With Quote
 
 
 
 
Alexander Schmolck
Guest
Posts: n/a
 
      01-08-2004
"John Roth" <(E-Mail Removed)> writes:
> What's happening here?


cf.

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

'as
 
Reply With Quote
 
Michael Hudson
Guest
Posts: n/a
 
      01-08-2004
"John Roth" <(E-Mail Removed)> writes:

[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

--
6. Symmetry is a complexity-reducing concept (co-routines include
subroutines); seek it everywhere.
-- Alan Perlis, http://www.cs.yale.edu/homes/perlis-alan/quotes.html
 
Reply With Quote
 
Mirko Zeibig
Guest
Posts: n/a
 
      01-08-2004
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
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      01-08-2004
John Roth wrote:

> 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

 
Reply With Quote
 
John Roth
Guest
Posts: n/a
 
      01-08-2004

"Michael Hudson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "John Roth" <(E-Mail Removed)> writes:
>
> [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.


Arrrgggh! Of course!

John Roth
>
> Cheers,
> mwh
>



 
Reply With Quote
 
Francis Avila
Guest
Posts: n/a
 
      01-08-2004
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.
--
Francis Avila

 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      01-08-2004

"John Roth" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
John Roth
Guest
Posts: n/a
 
      01-08-2004

"Terry Reedy" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "John Roth" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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
>
> Terry J. Reedy
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
super.clone() puzzlement Derek Fountain Java 42 11-27-2008 10:40 AM
String#scan puzzlement Martin DeMello Ruby 2 07-12-2006 12:59 PM
puzzlement about classmethod Faheem Mitha Python 2 06-24-2006 09:34 PM
(?{ ... }) puzzlement J Krugman Perl Misc 4 05-31-2004 11:05 PM
Read/Write IO on socket file descriptor? Patrick LeBoutillier Perl 0 07-19-2003 05:25 PM



Advertisments