__call__

M

Mike Meyer

TK said:
Sorry but it does not work. Here's my code:

... def __call__(self):
... print 'Hi'
...
<__main__.Test object at 0x3e6d0>

Test() invokes the class, which returns an instance of the class.

Look back at what Simon did. He stored the value returned by Test() as
t, then called t with "t()". That's what __call__ is - the routine
invoked when an instance of a class is invoked as a callable object.

To change what happens when the class is invoked as a callable object,
you need to fool with metaclasses.

It might help if you told us what you really wanted to do with this
construct.

<mike
 
T

tiissa

TK said:
Sorry but it does not work.

It _does_ work. It just don't do what you expect. That's different till
you know how to do it.

Let's see:
> ... def __call__(self):
> ... print 'Hi'
> ...

You first define a class whose members can be called.
> <__main__.Test object at 0x3e6d0>

Then you build an instance of these class. Fine.

If you want to call this instance you have to tell python:
.... def __init__(self):
.... print 'In init.'
.... def __call__(self):
.... print 'In call.'
....In init.
In init.
In call.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top