how can I use a callable object as a method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Piotr Sobolewski

    #1

    how can I use a callable object as a method

    Hello,

    I would like to use a callable object as a method of a class. So, when I
    have such normal class:

    class f:
    version = 17
    def a(self):
    return self.version

    f1 = f()
    print f1.a()


    I want to change it to something like that:

    class add:
    def __call__(self, another_self):
    return another_self.ve rsion

    class f:
    version = 17
    a = add()

    f1 = f()
    print f1.a()

    However, the second version does not work. I think I understand why. That's
    because "a" inside f1 is not a function (but an object). So f1.a is not a
    method. So when I do f1.a(), the implicit argument self is not passed.

    Q1: Am I right? Is this the problem?
    Q2: What can I do to make it work?

  • Marco Wahl

    #2
    Re: how can I use a callable object as a method

    Piotr Sobolewski <NIE_DZIALA@gaz eta.plwrites:
    I would like to use a callable object as a method of a class. So, when I
    have such normal class:
    >
    I want to change it to something like that:
    >
    class add:
    def __call__(self, another_self):
    return another_self.ve rsion
    >
    class f:
    version = 17
    a = add()
    >
    f1 = f()
    print f1.a()
    >
    However, the second version does not work. I think I understand why. That's
    because "a" inside f1 is not a function (but an object). So f1.a is not a
    method. So when I do f1.a(), the implicit argument self is not passed.
    >
    Q1: Am I right? Is this the problem?
    Q2: What can I do to make it work?
    Use the right argument for the call.

    Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
    Type "help", "copyright" , "credits" or "license" for more information.
    >>class add:
    def __call__(self, another_self):
    return another_self.ve rsion

    .... ... ... >>class f:
    version = 17
    a = add()

    .... ... ... >>f1 = f()
    >>f1
    <__main__.f instance at 0x00A805D0>
    >>f1.a
    <__main__.add instance at 0x00A80DA0>
    >>f1.a()
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: __call__() takes exactly 2 arguments (1 given)
    >>f1.a(f1)
    17
    >>>

    HTH

    Comment

    • Hrvoje Niksic

      #3
      Re: how can I use a callable object as a method

      Piotr Sobolewski <NIE_DZIALA@gaz eta.plwrites:
      However, the second version does not work. I think I understand
      why. That's because "a" inside f1 is not a function (but an object).
      An object that defines __call__ is perfectly usable as a function.
      Your problem is that it doesn't know how to convert itself to a
      method, so that f1.a() knows how to pass f1 as another_self to
      add.__call__. To do that, add needs to be a bit smarter:
      >>class add(object):
      .... def __call__(self, another_self):
      .... return another_self.ve rsion
      .... def __get__(self, obj, type=None):
      .... return lambda: self(obj)
      ....
      >>class f(object):
      .... version = 17
      .... a = add()
      ....
      >>f1 = f()
      >>f1.a()
      17

      If you can't modify add, you can probably use an adaptor that defines
      __get__ in a similar way.

      Comment

      • Piotr Sobolewski

        #4
        Re: how can I use a callable object as a method

        Hrvoje Niksic wrote:
        >However, the second version does not work. I think I understand
        >why. That's because "a" inside f1 is not a function (but an object).
        >
        An object that defines __call__ is perfectly usable as a function.
        Your problem is that it doesn't know how to convert itself to a
        method, so that f1.a() knows how to pass f1 as another_self to
        add.__call__. To do that, add needs to be a bit smarter:
        A! Now it works, thanks a lot!

        Comment

        Working...