newbie question, when __call__ method is used?

Discussion in 'Python' started by chenyu, Oct 27, 2003.

  1. chenyu

    chenyu Guest

    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
     
    chenyu, Oct 27, 2003
    #1
    1. Advertising

  2. chenyu

    KefX Guest

    >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
     
    KefX, Oct 27, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patrick Lioi
    Replies:
    7
    Views:
    353
    Beni Cherniavsky
    Aug 19, 2003
  2. Steve Holden

    Re: instance has no __call__ method

    Steve Holden, Dec 11, 2010, in forum: Python
    Replies:
    0
    Views:
    1,233
    Steve Holden
    Dec 11, 2010
  3. Marco
    Replies:
    1
    Views:
    180
    Ian Kelly
    Nov 20, 2012
  4. Marco Buttu
    Replies:
    3
    Views:
    145
    Marco Buttu
    Oct 7, 2013
  5. Yaþar Arabacý

    How to np.vectorize __call__ method

    Yaþar Arabacý, Nov 14, 2013, in forum: Python
    Replies:
    0
    Views:
    112
    Yaþar Arabacý
    Nov 14, 2013
Loading...

Share This Page