By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,932 Members | 1,944 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,932 IT Pros & Developers. It's quick & easy.

Understanding __call__ in Python 3

P: 20
I am reading a Python book for Pyton 2.5 (But I am doing Python 3). I am at the chapter classes and I got this part;

"You can check whenever the function attribute was callable."

Expand|Select|Wrap|Line Numbers
  1. callable(tc,'talk',None)
In Python3 we do not have callable anymore, so I checked on the internet, how do it, and I found this:

Expand|Select|Wrap|Line Numbers
  1. hasattr(anything, '__call__')
Now here are my question:

1.) What do I have to put in the "anything" part? a function of a class?

2.) What does callable actually do? What does callable actually means? To check when was the last time I used that function..? What does it do? And what kind of situatins would I use this?

Thanks in advance
May 18 '11 #1
Share this Question
Share on Google+
2 Replies

Expert 100+
P: 621
This has already been answered in another forum.
May 18 '11 #2

Expert Mod 2.5K+
P: 2,851
"A function call is an expression containing a simple type name and a parenthesized argument list."

Builtin function callable() returns True if an object has a __call__ method or False if not.
Expand|Select|Wrap|Line Numbers
  1. >>> callable
  2. <built-in function callable>
  3. >>> callable(str)
  4. True
  5. >>> hasattr(str, "__call__")
  6. True
  7. >>> x = 10
  8. >>> callable(x)
  9. False
  10. >>> 
May 23 '11 #3

Post your reply

Sign in to post your reply or Sign up for a free account.