'class' argument optional for new.instancemethod?

Discussion in 'Python' started by Martin Miller, Mar 12, 2005.

  1. In section "3.27 new -- Creation of runtime internal objects" of the
    documentation that comes with Python 2.4 it says:

    > instancemethod(function, instance, class)
    >
    > This function will return a method object, bound to instance, or

    unbound if
    > instance is None. function must be callable.


    However, some simple experiments I've tried seem to indicate that the
    last argument, 'class' can be left off with no ill effects, and is
    therefore optional.

    Doesn't anyone know if this is a documention problem? Personally, I
    don't understand how the argument gets used when it *is* provided -- if
    nothing else, having it seems somewhat redundant given the presence of
    the 'instance' argument.

    TIA,
    Martin
     
    Martin Miller, Mar 12, 2005
    #1
    1. Advertising

  2. Martin Miller

    Peter Hansen Guest

    Martin Miller wrote:
    > In section "3.27 new -- Creation of runtime internal objects" of the
    > documentation that comes with Python 2.4 it says:
    >
    >>instancemethod(function, instance, class)

    [...]
    > However, some simple experiments I've tried seem to indicate that the
    > last argument, 'class' can be left off with no ill effects, and is
    > therefore optional.
    >
    > Doesn't anyone know if this is a documention problem? Personally, I
    > don't understand how the argument gets used when it *is* provided -- if
    > nothing else, having it seems somewhat redundant given the presence of
    > the 'instance' argument.


    A little experimentation reveals at least one thing that
    argument affects:

    >>> import new
    >>> class A:

    .... pass
    ....
    >>> class B:

    .... pass
    ....
    >>> def f(self):

    .... print 'in f', self
    ....
    >>> a = A()
    >>> a.f = new.instancemethod(f, a)
    >>> a.f()

    in f <__main__.A instance at 0x00AE5EB8>
    >>> a.g = new.instancemethod(f, a, B)
    >>> a.g()

    in f <__main__.A instance at 0x00AE5EB8>
    >>> a.g

    <bound method B.f of <__main__.A instance at 0x00AE5EB8>>
    >>> a.f

    <bound method ?.f of <__main__.A instance at 0x00AE5EB8>>

    So without the class argument, at least in this small
    sample, the bound method doesn't know what class
    it belongs to, at least for purposes of printing
    it's own repr().

    -Peter
     
    Peter Hansen, Mar 12, 2005
    #2
    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. Replies:
    4
    Views:
    310
  2. schickb

    new.instancemethod questions

    schickb, Jan 30, 2009, in forum: Python
    Replies:
    4
    Views:
    772
    schickb
    Jan 30, 2009
  3. Replies:
    3
    Views:
    717
    Michele Simionato
    Apr 5, 2009
  4. Replies:
    0
    Views:
    300
  5. Martin Drautzburg

    new.instancemethod __iter__

    Martin Drautzburg, Feb 6, 2010, in forum: Python
    Replies:
    7
    Views:
    300
    Steve Holden
    Feb 7, 2010
Loading...

Share This Page