__call__ considered harmful or indispensable?

Duncan Booth duncan.booth at invalid.invalid
Fri Aug 3 09:56:07 EDT 2007


skip at pobox.com wrote:

> In this particular case it was clearly unnecessary and just obfuscated
> the code.  I'm wondering, are there some general cases where __call__
> methods of a user-defined class are simply indispensable?

I don't know that you couldn't live without __call__, but it would make 
some code harder to write and understand. The time you need __call__ is 
when you need an object which is callable and which also has methods. e.g. 
look at xmlrpclib:

server = ServerProxy('http://some.server.com/path')

server.method(x=5) retrieves the url http://some.server.com/path/method?x=5
server.method.submethod(x=5) retrieves the url 
http://some.server.com/path/method/submethod?x=5


There is no need here to distinguish between a callable object and an 
object used for traversal. I think without __call__, code to access web 
servers would be less clean.



More information about the Python-list mailing list