Descriptor puzzlement

Discussion in 'Python' started by John Roth, Jan 8, 2004.

  1. John Roth

    John Roth Guest

    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
    John Roth, Jan 8, 2004
    #1
    1. Advertising

  2. "John Roth" <> 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")
    Jacek Generowicz, Jan 8, 2004
    #2
    1. Advertising

  3. "John Roth" <> writes:
    > What's happening here?


    cf.

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

    'as
    Alexander Schmolck, Jan 8, 2004
    #3
  4. "John Roth" <> 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
    Michael Hudson, Jan 8, 2004
    #4
  5. John Roth

    Mirko Zeibig Guest

    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
    Mirko Zeibig, Jan 8, 2004
    #5
  6. John Roth

    Peter Otten Guest

    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
    Peter Otten, Jan 8, 2004
    #6
  7. John Roth

    John Roth Guest

    "Michael Hudson" <> wrote in message
    news:...
    > "John Roth" <> 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
    >
    John Roth, Jan 8, 2004
    #7
  8. 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
    Francis Avila, Jan 8, 2004
    #8
  9. John Roth

    Terry Reedy Guest

    "John Roth" <> wrote in message
    news:...
    > 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
    Terry Reedy, Jan 8, 2004
    #9
  10. John Roth

    John Roth Guest

    "Terry Reedy" <> wrote in message
    news:...
    >
    > "John Roth" <> wrote in message
    > news:...
    > > 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
    >
    >
    John Roth, Jan 8, 2004
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patrick LeBoutillier

    Read/Write IO on socket file descriptor?

    Patrick LeBoutillier, Jul 19, 2003, in forum: Perl
    Replies:
    0
    Views:
    3,056
    Patrick LeBoutillier
    Jul 19, 2003
  2. Faheem Mitha

    puzzlement about classmethod

    Faheem Mitha, Jun 24, 2006, in forum: Python
    Replies:
    2
    Views:
    257
    Dennis Lee Bieber
    Jun 24, 2006
  3. Derek Fountain

    super.clone() puzzlement

    Derek Fountain, Nov 20, 2008, in forum: Java
    Replies:
    42
    Views:
    1,316
    RedGrittyBrick
    Nov 27, 2008
  4. Martin DeMello

    String#scan puzzlement

    Martin DeMello, Jul 12, 2006, in forum: Ruby
    Replies:
    2
    Views:
    81
    Martin DeMello
    Jul 12, 2006
  5. J Krugman

    (?{ ... }) puzzlement

    J Krugman, May 31, 2004, in forum: Perl Misc
    Replies:
    4
    Views:
    126
    Matt Garrish
    Jun 1, 2004
Loading...

Share This Page