Descriptor puzzlement

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Roth

    Descriptor puzzlement

    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(o bject):
    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\M y Documents\Proje cts\AstroPy4>b

    C:\Documents and Settings\John\M y Documents\Proje cts\AstroPy4>py thon b.py
    <__main__.AnObj ect object at 0x0086D248>
    foobar

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

    What's happening here?

    John Roth


  • Alexander Schmolck

    #2
    Re: Descriptor puzzlement

    "John Roth" <newsgroups@jhr othjr.com> writes:[color=blue]
    > What's happening here?[/color]

    cf.

    class AnotherObject(o bject):
    prop = AnObject("snafu ")

    'as

    Comment

    • Mirko Zeibig

      #3
      Re: Descriptor puzzlement

      John Roth said the following on 01/08/2004 01:34 PM:[color=blue]
      > 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[/color]
      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)
      [color=blue]
      > class AnotherObject(o bject):
      > def __init__(self):
      > self.prop = AnObject("snafu ")
      >
      > myClass = AnotherObject()
      > print myClass.prop[/color]
      Now just do:
      print id(myClass.prop ) to see the internal reference.
      [color=blue]
      > myClass.prop = "foobar"[/color]
      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.
      [color=blue]
      > print myClass.prop[/color]

      Regards
      Mirko

      Comment

      • Peter Otten

        #4
        Re: Descriptor puzzlement

        John Roth wrote:
        [color=blue]
        > 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(o bject):
        > 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\M y Documents\Proje cts\AstroPy4>b
        >
        > C:\Documents and Settings\John\M y Documents\Proje cts\AstroPy4>py thon b.py
        > <__main__.AnObj ect object at 0x0086D248>
        > foobar
        >
        > It doesn't look like the descriptor protocol is getting
        > invoked at all.
        >
        > What's happening here?[/color]

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

        class AnotherObject(o bject):
        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

        Comment

        • Michael Hudson

          #5
          Re: Descriptor puzzlement

          "John Roth" <newsgroups@jhr othjr.com> writes:

          [snippety]
          [color=blue]
          > It doesn't look like the descriptor protocol is getting
          > invoked at all.
          >
          > What's happening here?[/color]

          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

          Comment

          • John Roth

            #6
            Re: Descriptor puzzlement


            "Michael Hudson" <mwh@python.net > wrote in message
            news:m34qv6388c .fsf@pc150.math s.bris.ac.uk...[color=blue]
            > "John Roth" <newsgroups@jhr othjr.com> writes:
            >
            > [snippety]
            >[color=green]
            > > It doesn't look like the descriptor protocol is getting
            > > invoked at all.
            > >
            > > What's happening here?[/color]
            >
            > Descriptors need to be attached to classes.[/color]

            Arrrgggh! Of course!

            John Roth[color=blue]
            >
            > Cheers,
            > mwh
            >[/color]


            Comment

            • Jacek Generowicz

              #7
              Re: Descriptor puzzlement

              "John Roth" <newsgroups@jhr othjr.com> writes:
              [color=blue]
              > It doesn't look like the descriptor protocol is getting
              > invoked at all.
              >
              > What's happening here?[/color]

              Try changing the following
              [color=blue]
              > class AnotherObject(o bject):
              > def __init__(self):
              > self.prop = AnObject("snafu ")[/color]

              to

              class AnotherObject(o bject):
              prop = AnObject("snafu ")

              Comment

              • Francis Avila

                #8
                Re: Descriptor puzzlement

                Mirko Zeibig wrote in message ...[color=blue]
                >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)[/color]

                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

                Comment

                • Terry Reedy

                  #9
                  Re: Descriptor puzzlement


                  "John Roth" <newsgroups@jhr othjr.com> wrote in message
                  news:vvqjj92il4 o1d0@news.super news.com...[color=blue]
                  > 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[/color]

                  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


                  Comment

                  • John Roth

                    #10
                    Re: Descriptor puzzlement


                    "Terry Reedy" <tjreedy@udel.e du> wrote in message
                    news:NaudnQjKFt 8IOmCiRVn-tw@comcast.com. ..[color=blue]
                    >
                    > "John Roth" <newsgroups@jhr othjr.com> wrote in message
                    > news:vvqjj92il4 o1d0@news.super news.com...[color=green]
                    > > 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[/color]
                    >
                    > Did you mean to set self.it to match the __init__ and __get__ methods?
                    > Or am I missing something about the esoterics of properties?[/color]

                    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[color=blue]
                    >
                    > Terry J. Reedy
                    >
                    >[/color]


                    Comment

                    Working...