473,324 Members | 2,214 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,324 software developers and data experts.

__call__

TK
Hi,

how can handle __call__? Can you give a sample?

Thanks

o-o

Thomas
Jul 19 '05 #1
6 2677
Look at http://docs.python.org/ref/callable-types.html
class Test(object): .... def __call__(self):
.... print "the instance was called"
.... t = Test()
t()

the instance was called

Is this what you wanted?

Jul 19 '05 #2
TK
>>>>class Test(object):

... def __call__(self):
... print "the instance was called"
...
t = Test()
t()


the instance was called

Is this what you wanted?


Yeah! Thanks.
Jul 19 '05 #3
TK
Simon Percivall wrote:
Look at http://docs.python.org/ref/callable-types.html

class Test(object):
... def __call__(self):
... print "the instance was called"
...
t = Test()
t()
the instance was called

Is this what you wanted?


Sorry but it does not work. Here's my code:
class Test(object): .... def __call__(self):
.... print 'Hi'
.... Test()

<__main__.Test object at 0x3e6d0>
o-o

Thomas

Jul 19 '05 #4
TK <me****@o2online.de> writes:
Simon Percivall wrote:
Look at http://docs.python.org/ref/callable-types.html
>class Test(object):

... def __call__(self):
... print "the instance was called"
...
>t = Test()
>t()

the instance was called
Is this what you wanted?


Sorry but it does not work. Here's my code:
>>> class Test(object): ... def __call__(self):
... print 'Hi'
... >>> Test()

<__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
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 19 '05 #5
TK wrote:
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:
>>> class Test(object): ... def __call__(self):
... print 'Hi'
...


You first define a class whose members can be called.
>>> Test() <__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:
class Test: .... def __init__(self):
.... print 'In init.'
.... def __call__(self):
.... print 'In call.'
.... t=Test() In init. t() In call. Test() In init.
<__main__.Test instance at 0x401e2b6c> Test()() In init.
In call.

Jul 19 '05 #6
TK
tiissa wrote:
TK wrote:
Sorry but it does not work.

It _does_ work. It just don't do what you expect.


Seems so;-).

Thanks a lot for help!!!

o-o

Thomas
Jul 19 '05 #7

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

Similar topics

7
by: Patrick Lioi | last post by:
def foo(): pass foo is a function foo is a callable object foo has method __call__ defined foo.__call__ is a function foo.__call__ is a callable object foo.__call__ has method __call__...
31
by: John Roth | last post by:
I'm adding a thread for comments on Gerrit Holl's pre-pep, which can be found here: http://tinyurl.com/2578q Frankly, I like the idea. It's about time that all of the file and directory stuff...
5
by: Robert Ferrell | last post by:
I have a question about assigning __call__ to an instance to make that instance callable. I know there has been quite a bit of discussion about this, and I've read all I can find, but I'm still...
11
by: Stefan Behnel | last post by:
Hi! This somewhat puzzles me: Python 2.4 (#1, Feb 3 2005, 16:47:05) on linux2 Type "help", "copyright", "credits" or "license" for more information. ..>>> class test(object): .... def...
8
by: Rahul | last post by:
Consider the following: def a(x): return x+1 def b(f): def g(*args,**kwargs): for arg in args: print arg return f(*args,**kwargs) return g
7
by: ncf | last post by:
I have a feeling that this is highly unlikely, but does anyone in here know if it's possible to directly call a module, or will I have to wrap it up in a class? i.e., import MyMod...
5
by: Kent Johnson | last post by:
I am learning about metaclasses and there is something that confuses me. I understand that if I define a __call__ method for a class, then instances of the class become callable using function...
7
by: Gigs_ | last post by:
from Tkinter import * from tkFileDialog import askopenfilename from tkColorChooser import askcolor from tkMessageBox import askquestion, showerror from tkSimpleDialog import askfloat demos...
17
by: skip | last post by:
I don't personally use __call__ methods in my classes, but I have encountered it every now and then here at work in code written by other people. The other day I replaced __call__ with a more...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.