Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > newbie question, when __call__ method is used?

Reply
Thread Tools

newbie question, when __call__ method is used?

 
 
chenyu
Guest
Posts: n/a
 
      10-27-2003
Hi everyone,
I have read other's code, and found it contains the __call__. But I
don't know when its code will be executed.
From the Python manual, I know it is built-in function and is a
callback function. But the manual doesn't contain any simple example.

Could you give me an simple example to explain when its code will be
executed?



Thank you for your attention.
kind regards/chenyu
 
Reply With Quote
 
 
 
 
KefX
Guest
Posts: n/a
 
      10-27-2003
>I have read other's code, and found it contains the __call__. But I
>don't know when its code will be executed.


It will be executed when a class instance is executed as a function.

For example:
foobar = MyClass()
foobar(2)

This is the same as:
foobar = MyClass()
foobar.__call__(2)

One way of looking at it is that it's simple shorthand, but there's also
another advantage (which is the real reason it's there): it turns your class
instance into a 'callable', that is, almost anything that requires a function
can also take your class instance (in general).

- Kef
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The type.__call__() method manages the calls to __new__ and __init__? Marco Python 1 11-20-2012 07:25 PM
Re: instance has no __call__ method Steve Holden Python 0 12-11-2010 02:24 PM
Redefining __call__ in an instance Robert Ferrell Python 5 01-21-2004 09:34 PM
RE: Redefining __call__ in an instance Robert Brewer Python 1 01-16-2004 02:58 AM
Why doesn't __call__ lead to infinite recursion? Patrick Lioi Python 7 08-19-2003 06:41 PM



Advertisments