473,387 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

Sep 18 '08 #1
3 995
Piotr Sobolewski <NI********@gazeta.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.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?
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.version

.... ... ... >>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
Sep 18 '08 #2
Piotr Sobolewski <NI********@gazeta.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.version
.... 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.
Sep 18 '08 #3
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!

Sep 23 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Roy Smith | last post by:
I've got a function which gets passed a call-back function as a parameter. I want to check to make sure the thing passed in is indeed callable. Is: assert '__call__' in dir (param) the...
2
by: David Eppstein | last post by:
Suppose I have a callable object x, and suppose for simplicity that it doesn't take any *- or **-arguments. How can I get a list of the number and names of the arguments of x? If x is a...
4
by: Michal Vitecek | last post by:
hello everyone, today i've come upon a strange exception, consider the following file test.py: --- beginning of test.py --- class A(object): def method1(parA): print "in A.method1()"
7
by: Edward Diener | last post by:
This simple code example gives me the message, "TypeError: 'staticmethod' object is not callable". class X(object): def Y(x): print x Y = staticmethod(Y) ad = { 1 : Y } def Z(self):...
28
by: The Eternal Squire | last post by:
PEP: 336 Title: Make None Callable Version: $Revision: 1.1 $ Last-Modified: $Date: 2004/11/03 16:58:30 $ Author: Andrew McClelland Status: Draft Type: Standards Track Content-Type: text/plain...
0
by: Steven Bethard | last post by:
Steven Bethard wrote: > (For anyone else out there reading who doesn't already know this, > Steven D'Aprano's comments are easily explained by noting that the > __get__ method of staticmethod...
1
by: Gary Wessle | last post by:
dear python users I am not sure why I am getting **************************************************************** Traceback (most recent call last): File "my.py", line 3, in ?...
6
by: Ron Garret | last post by:
If I do this: def f(self): print self class c1: pass setattr(c1, 'm1', f) Then f is automagically transmogrified into the appropriate sort of method depending on how it is used:
6
by: netimen | last post by:
Can I substitute a method of a class by a callable object (not a function)? I can very easy insert my function in a class as a method, but an object - can't. I have the following: class...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.