how can I use a callable object as a method

Piotr Sobolewski NIE_DZIALA at gazeta.pl
Thu Sep 18 06:26:52 EDT 2008


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.version

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?




More information about the Python-list mailing list