class objects, method objects, function objects

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • 7stud

    #1

    class objects, method objects, function objects

    Hi,

    I'm trying to figure out what this passage from GvR's tutorial means:
    -------
    Class definitions, like function definitions (def statements) must be
    executed before they have any effect....

    When a class definition is entered, a new namespace is created...

    When a class definition is left normally (via the end), a class object
    is created.

    If you still don't understand how methods work, a look at the
    implementation can perhaps clarify matters. When an instance attribute
    is referenced that isn't a data attribute, its class is searched. If
    the name denotes a valid class attribute that is a function object, a
    method object is created by packing (pointers to) the instance object
    and the function object just found together in an abstract object:
    this is the method object.
    -----------

    Here is my diagram of the above:

    class object
    ----------------------------------------
    | class MyClass: |
    | def __init__(self): |
    | self.name = "GvR" |
    | def sayHi(self): |
    | print "Hello " + self.name |
    |______________ _______________ ___________|


    x = MyClass()
    x.sayHi() #the "class search" begins which initiates the
    #creation of the method object:


    method object instance object
    ----------------- --------------
    | ptr1 ----------|--------|x = MyClass() |
    | ptr2 | |______________ |
    | | |
    |___|__________ ___|
    |
    |
    V
    function object
    ---------------------------------
    | def sayHi(self): print "hello" |
    |______________ _______________ ____|

    But the last part of the passage makes no sense to me:
    ------
    When the method object is called with an argument list, it is unpacked
    again, a new argument list is constructed from the instance object and
    the original argument list, and the function object is called with
    this new argument list.
    ------
    Can anyone interpret that for me?

  • 7stud

    #2
    Re: class objects, method objects, function objects

    Oops, here is that last sentence in context(section 9.3.4):
    ------------
    What exactly happens when a method is called? You may have noticed
    that x.f() was called without an argument above, even though the
    function definition for f specified an argument. What happened to the
    argument? Surely Python raises an exception when a function that
    requires an argument is called without any -- even if the argument
    isn't actually used...

    Actually, you may have guessed the answer: the special thing about
    methods is that the object is passed as the first argument of the
    function. In our example, the call x.f() is exactly equivalent to
    MyClass.f(x). In general, calling a method with a list of n arguments
    is equivalent to calling the corresponding function with an argument
    list that is created by inserting the method's object before the first
    argument.

    If you still don't understand how methods work, a look at the
    implementation can perhaps clarify matters. When an instance attribute
    is referenced that isn't a data attribute, its class is searched. If
    the name denotes a valid class attribute that is a function object, a
    method object is created by packing (pointers to) the instance object
    and the function object just found together in an abstract object:
    this is the method object. When the method object is called with an
    argument list, it is unpacked again, a new argument list is
    constructed from the instance object and the original argument list,
    and the function object is called with this new argument list.
    ------------

    Comment

    • 7stud

      #3
      Re: class objects, method objects, function objects

      Darn. I made some changes to the class and I didn't change the
      function object. It should be:

      |
      V
      function object
      ---------------------------------
      | def sayHi(self): |
      | print "Hello " + self.name|
      |______________ _______________ ____|


      Comment

      • Gabriel Genellina

        #4
        Re: class objects, method objects, function objects

        En Mon, 19 Mar 2007 01:38:37 -0300, 7stud <bbxx789_05ss@y ahoo.com>
        escribió:
        But the last part of the passage makes no sense to me:
        ------
        When the method object is called with an argument list, it is unpacked
        again, a new argument list is constructed from the instance object and
        the original argument list, and the function object is called with
        this new argument list.
        ------
        Can anyone interpret that for me?
        Calling a.foo(x,y) means that, after fetching foo from a, a new argument
        list must be constructed containing 3 items: the instance a (self), x and
        y.

        --
        Gabriel Genellina

        Comment

        • Alex Martelli

          #5
          Re: class objects, method objects, function objects

          7stud <bbxx789_05ss@y ahoo.comwrote:
          ...
          But the last part of the passage makes no sense to me:
          ------
          When the method object is called with an argument list, it is unpacked
          again, a new argument list is constructed from the instance object and
          the original argument list, and the function object is called with
          this new argument list.
          ------
          Can anyone interpret that for me?
          Here's the Python pseudocode equivalent...

          class Method(object):
          def __init__(self, im_func, im_self):
          self.im_func = im_func
          self.im_self = im_self
          def __call__(self, *a, **k):
          return self.im_func(se lf.im_self, *a, **k)

          Whenever you get foo.somemeth from an instance foo of whatever class
          (with somemeth being a method of that class), you obtain the equivalent
          of a Method(type(foo ).__dict__('som emeth'), foo) in the above
          pseudocode. When you call that foo.somemeth, the im_self (i.e., foo) is
          prepended to other positional arguments (if any).

          This, btw, takes place in the __get__ method of the function object
          somemeth -- function objects are descriptors, i.e., they have __get__
          methods, and that's just what they do.

          If this is still a bit obscure to you, perhaps more practice with Python
          may be needed before you dwell into the internals of methods &c.


          Alex

          Comment

          • Fredrik Lundh

            #6
            Re: class objects, method objects, function objects

            "7stud" wrote:
            But the last part of the passage makes no sense to me:
            ------
            When the method object is called with an argument list, it is unpacked
            again, a new argument list is constructed from the instance object and
            the original argument list, and the function object is called with
            this new argument list.
            ------
            Can anyone interpret that for me?
            when you call obj.method(arg1 , arg2), Python prepends the actual instance
            object (obj) to the argument list, so you end up calling (obj.method) with the
            arguments (obj, arg1, arg2).

            </F>



            Comment

            • 7stud

              #7
              Re: class objects, method objects, function objects

              Hi,

              Thanks for the responses. I understand that python automatically
              sends 'self' to a member function, i.e. self gets prepended to the
              argument list. I guess I am having trouble with this statement:
              ----
              When the method object is called with an
              argument list, it is unpacked again, a new argument list is
              constructed from the instance object and the original argument list,
              and the function object is called with this new argument list.
              ---
              because I don't quite understand the timing. The "unpacked again"
              part confuses me. If I have this code:

              class MyClass:
              def g(self, name):
              print "Hello " + name

              x = MyClass()
              x.g("GvR")

              here is what happens when I try to follow the sequence of events GvR
              describes:
              ----------------
              When an instance attribute is referenced that isn't a data attribute,
              its class is searched.

              x.g("GvR") =='g' is not a data attribute, so class is searched.

              If the name denotes a valid class attribute that is a function object,
              a
              method object is created by packing (pointers to) the instance object
              and the function object just found together in an abstract object:
              this is the method object.

              ==>method object is created

              When the method object is called with an
              argument list,

              x.g("GvR") ==I think I'm both "referencin g an instance attribute"
              and calling the method object with an argument list

              it(the method object) is unpacked again, a new argument list is
              constructed from the instance object and the original argument list,
              and the function object is called with this new argument list.

              ??

              Comment

              • Duncan Booth

                #8
                Re: class objects, method objects, function objects

                "7stud" <bbxx789_05ss@y ahoo.comwrote:
                When the method object is called with an
                argument list,
                >
                x.g("GvR") ==I think I'm both "referencin g an instance attribute"
                and calling the method object with an argument list
                >
                it(the method object) is unpacked again, a new argument list is
                constructed from the instance object and the original argument list,
                and the function object is called with this new argument list.
                It may help you to understand if you consider the two parts separately.
                There is no linkage between 'referencing an instance attribute' and
                'calling the method object'. You can rewrite the code as:

                method = x.g
                method("GvR")

                and it works in exactly the same way. First you reference the attribute
                'x.g', which looks up MyClass.g (an unbound method) and calls
                MyClass.g.__get __(x, MyClass) which creates a new bound method. Then you
                call the bound method passing "GvR" as the first positional argument: so a
                single element tuple is created for the argument list.

                The bound method object unpacks and repacks the tuple to give the two-tuple
                (self, "GvR") which it then passes to the original function.

                Comment

                • Fredrik Lundh

                  #9
                  Re: class objects, method objects, function objects

                  Dennis Lee Bieber wrote:
                  "it(the" -- argument list, not the object -- ") is unpacked again
                  no, "it" refers to the bound method object, as 7stud would have realized if
                  he'd read the entire paragraph. here's the relevant portion:

                  /.../ a method object is created by packing (pointers to) the instance object
                  and the function object just found together in an abstract object: this is the
                  method object. When the method object is called with an argument list, it
                  is unpacked again /.../

                  </F>



                  Comment

                  • 7stud

                    #10
                    Re: class objects, method objects, function objects

                    Thanks Duncan and Dennis.

                    Comment

                    Working...