Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #106613

Re: Promoting Python

From Marko Rauhamaa <marko@pacujo.net>
Newsgroups comp.lang.python
Subject Re: Promoting Python
Date 2016-04-07 09:30 +0300
Organization A noiseless patient Spider
Message-ID <87r3eihqvx.fsf@elektro.pacujo.net> (permalink)
References (15 earlier) <CALwzid=5eNxPOFm1zDe_wXSbW_HM1trO-1YxrpA7qrTQ0p9AiA@mail.gmail.com> <mailman.43.1459978690.1197.python-list@python.org> <87wpoaiedl.fsf@elektro.pacujo.net> <57059E6D.8070105@roce.be> <mailman.56.1459992022.1197.python-list@python.org>

Show all headers | View raw


Rolf Camps <rolf@roce.be>:

> Op 07-04-16 om 00:03 schreef Marko Rauhamaa:
>> IOW, if I have this class:
>>
>>      class A:
>>          def f(self):
>>              print("f")
>>
>> and this object:
>>
>>      a = A()
>>
>> then,
>>
>>      a.f
>>
>> is a function that doesn't have a self argument. That function is
>> generated on the fly, and it delegates to A.f, providing it with self
>> argument.
> a.f is not a function, A.f is a function.
> a.f is an instance method. The function is not generated on the fly,
> when the method is called, it calls the function A.f with an extra
> argument __self__ (the instance a) inserted before the argument list the
> instance method was called with.

What you call an instance method *is* a function, meaning it is bound to
a callable object that in almost no outward way differs from any other
function.

It is generated on the fly:

   >>> id(a.f)
   140059683793288
   >>> id(a.f)
   140059683793864
   >>> a.f is a.f
   False

You are correct that the generated function is a simple trampoline that
supplies a self argument and delegates to A.f.

Or:

   When a class attribute reference (for class C, say) would yield a
   class method object, it is transformed into an instance method object
   whose __self__ attributes is C.
   <URL: https://docs.python.org/3/reference/datamodel.html?highlight=__g
   etattr__#the-standard-type-hierarchy>

So the only difference between a regular function and an instance method
object is the fact that the latter has a __self__ attribute set.

Although even that small difference can be paved over:

    def g():
        print("g")
    g.__self__ = a
    a.f = g


Marko

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Promoting Python "Gordon( Hotmail )" <sionet3344@hotmail.co.uk> - 2016-04-05 06:48 +0100
  Re: Promoting Python Rustom Mody <rustompmody@gmail.com> - 2016-04-05 00:31 -0700
    Re: Promoting Python Joel Goldstick <joel.goldstick@gmail.com> - 2016-04-05 08:06 -0400
      Re: Promoting Python alister <alister.ware@ntlworld.com> - 2016-04-05 18:02 +0000
        Re: Promoting Python BartC <bc@freeuk.com> - 2016-04-05 19:47 +0100
          Re: Promoting Python alister <alister.ware@ntlworld.com> - 2016-04-05 19:38 +0000
    Re: Promoting Python Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-04-05 08:13 -0400
      Re: Promoting Python Marko Rauhamaa <marko@pacujo.net> - 2016-04-05 15:31 +0300
  Re: Promoting Python Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-04-06 20:52 +1200
    Re: Promoting Python Marko Rauhamaa <marko@pacujo.net> - 2016-04-06 12:12 +0300
  Re: Promoting Python BartC <bc@freeuk.com> - 2016-04-06 12:06 +0100
    Re: Promoting Python Ned Batchelder <ned@nedbatchelder.com> - 2016-04-06 04:38 -0700
      Re: Promoting Python BartC <bc@freeuk.com> - 2016-04-06 14:21 +0100
    Re: Promoting Python Marko Rauhamaa <marko@pacujo.net> - 2016-04-06 14:46 +0300
      Re: Promoting Python Michael Selik <michael.selik@gmail.com> - 2016-04-06 13:33 +0000
        Re: Promoting Python Marko Rauhamaa <marko@pacujo.net> - 2016-04-06 17:14 +0300
          Re: Promoting Python Chris Angelico <rosuav@gmail.com> - 2016-04-07 00:20 +1000
            Re: Promoting Python Marko Rauhamaa <marko@pacujo.net> - 2016-04-06 21:23 +0300
              Re: Promoting Python Marko Rauhamaa <marko@pacujo.net> - 2016-04-06 21:50 +0300
              Re: Promoting Python Marko Rauhamaa <marko@pacujo.net> - 2016-04-06 22:30 +0300
          Re: Promoting Python Marko Rauhamaa <marko@pacujo.net> - 2016-04-06 22:22 +0300
            Re: Promoting Python Marko Rauhamaa <marko@pacujo.net> - 2016-04-06 22:59 +0300
              Re: Promoting Python Marko Rauhamaa <marko@pacujo.net> - 2016-04-06 23:39 +0300
                Re: Promoting Python Marko Rauhamaa <marko@pacujo.net> - 2016-04-07 01:03 +0300
                Re: Promoting Python Marko Rauhamaa <marko@pacujo.net> - 2016-04-07 09:30 +0300
                Re: Promoting Python Ian Kelly <ian.g.kelly@gmail.com> - 2016-04-07 00:56 -0600
                Re: Promoting Python Marko Rauhamaa <marko@pacujo.net> - 2016-04-07 10:19 +0300
                Re: Promoting Python Steven D'Aprano <steve@pearwood.info> - 2016-04-08 16:09 +1000
          Re: Promoting Python Marko Rauhamaa <marko@pacujo.net> - 2016-04-06 23:05 +0300
      Re: Promoting Python BartC <bc@freeuk.com> - 2016-04-06 14:54 +0100
        Re: Promoting Python Marko Rauhamaa <marko@pacujo.net> - 2016-04-06 17:08 +0300
          Re: Promoting Python Larry Martell <larry.martell@gmail.com> - 2016-04-06 10:36 -0400
        Re: Promoting Python Chris Angelico <rosuav@gmail.com> - 2016-04-07 00:14 +1000
        Re: Promoting Python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-04-06 15:20 +0100
          Re: Promoting Python Ned Batchelder <ned@nedbatchelder.com> - 2016-04-06 07:34 -0700
            Re: Promoting Python Ned Batchelder <ned@nedbatchelder.com> - 2016-04-06 10:55 -0700
              Re: Promoting Python BartC <bc@freeuk.com> - 2016-04-06 23:24 +0100
          Re: Promoting Python BartC <bc@freeuk.com> - 2016-04-06 18:04 +0100
    Re: Promoting Python Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-04-06 08:04 -0400
    Re: Promoting Python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-04-06 13:39 +0100
    Re: Promoting Python Steven D'Aprano <steve@pearwood.info> - 2016-04-07 03:40 +1000

csiph-web