Adding instance mthods

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fernando Rodriguez

    Adding instance mthods

    Hi,

    This sort of code works for staticmethods, but fails with instance methods.
    Why does it fail? O:-)

    [color=blue][color=green][color=darkred]
    >>> class C (object):[/color][/color][/color]
    def g(x,y):
    print 'static g', x, y
    g = staticmethod(g)
    [color=blue][color=green][color=darkred]
    >>> def h(self, x,y):[/color][/color][/color]
    return x+y
    [color=blue][color=green][color=darkred]
    >>> C.h = h
    >>> dir(C)[/color][/color][/color]
    ['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute __',
    '__hash__', '__init__', '__module__', '__new__', '__reduce__',
    '__reduce_ex__' , '__repr__', '__setattr__', '__str__', '__weakref__', 'g',
    'h'][color=blue][color=green][color=darkred]
    >>> C.h(3,4)[/color][/color][/color]

    Traceback (most recent call last):
    File "<pyshell#6 2>", line 1, in -toplevel-
    C.h(3,4)
    TypeError: unbound method h() must be called with C instance as first argument
    (got int instance instead)
  • Sean Ross

    #2
    Re: Adding instance mthods

    "Fernando Rodriguez" <frr@easyjob.ne t> wrote in message
    news:7k0nqvkvhf svvvubfv7iag56l 6i9n66eri@4ax.c om...[color=blue]
    > Hi,
    >
    > This sort of code works for staticmethods, but fails with instance[/color]
    methods.[color=blue]
    > Why does it fail? O:-)
    >
    >[color=green][color=darkred]
    > >>> class C (object):[/color][/color]
    > def g(x,y):
    > print 'static g', x, y
    > g = staticmethod(g)
    >[color=green][color=darkred]
    > >>> def h(self, x,y):[/color][/color]
    > return x+y
    >[color=green][color=darkred]
    > >>> C.h = h
    > >>> dir(C)[/color][/color]
    > ['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute __',
    > '__hash__', '__init__', '__module__', '__new__', '__reduce__',
    > '__reduce_ex__' , '__repr__', '__setattr__', '__str__', '__weakref__', 'g',
    > 'h'][color=green][color=darkred]
    > >>> C.h(3,4)[/color][/color]
    >
    > Traceback (most recent call last):
    > File "<pyshell#6 2>", line 1, in -toplevel-
    > C.h(3,4)
    > TypeError: unbound method h() must be called with C instance as first[/color]
    argument[color=blue]
    > (got int instance instead)[/color]


    Hi.
    It fails because "C.h = h" adds an unbound instance method to C, not a
    static method.
    To use an unbound instance method, you either need to call it on an instance
    of C
    (e.g., c.h(x,y)) or pass an instance of C as the first parameter (e.g.,
    C.h(c, x, y)).

    For example:
    [color=blue][color=green][color=darkred]
    >>> class C: pass[/color][/color][/color]
    ....[color=blue][color=green][color=darkred]
    >>> def f(self, x, y):[/color][/color][/color]
    .... return x + y
    ....[color=blue][color=green][color=darkred]
    >>> C.f = f
    >>> C.f[/color][/color][/color]
    <unbound method C.f>[color=blue][color=green][color=darkred]
    >>>
    >>> c = C()
    >>> c.f(2,3)[/color][/color][/color]
    5[color=blue][color=green][color=darkred]
    >>> C.f(c, 2,3)[/color][/color][/color]
    5[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]


    If you want to have the same behaviour as a static method, use
    staticmethod():
    [color=blue][color=green][color=darkred]
    >>> def g():[/color][/color][/color]
    .... print "hello"
    ....[color=blue][color=green][color=darkred]
    >>> C.g = g
    >>> C.g()[/color][/color][/color]
    Traceback (most recent call last):
    File "<interacti ve input>", line 1, in ?
    TypeError: unbound method g() must be called with C instance as first
    argument (got nothing instead)[color=blue][color=green][color=darkred]
    >>> C.g = staticmethod(g)
    >>> C.g()[/color][/color][/color]
    hello[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    HTH
    Sean


    Comment

    Working...