Understanding __call__ in Python 3

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ephexeve
    New Member
    • May 2011
    • 20

    Understanding __call__ in Python 3

    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."

    Code:
    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:

    Code:
    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
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    This has already been answered in another forum.

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      "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.
      Code:
      >>> callable
      <built-in function callable>
      >>> callable(str)
      True
      >>> hasattr(str, "__call__")
      True
      >>> x = 10
      >>> callable(x)
      False
      >>>

      Comment

      Working...