Python built-in function (--callable)

Source: Internet
Author: User

English documents:

callable(object)

Return True If the object argument appears callable, False if not. If this returns true, it's still possible that a call fails and if it is false, calling object would never Succe Ed. Note That classes is callable (calling a class returns a new instance); Instances is callable if their class has a __call__() method.

Detects whether an object can be called

Description

1. Method is used to detect whether an object can be called, which can be called by means of a method call that the object can use () parentheses.

>>> Callable (callable)
True
>>> Callable (1)
False
>>> 1 ()
Traceback (most recent):
File "<pyshell#5>", line 1, in <module>
1 ()
TypeError: ' int ' object is not callable
>>>

2. Callable objects may also fail to invoke the actual call, but cannot invoke the object, and the invocation must not succeed.

3. Class objects are callable objects, whether an instance object of a class can invoke an object, depending on whether the class defines the __call__ method.

>>> class A: #定义类A    pass>>> Callable (a) #类A是可调用对象True >>> a = A () #调用类A >>> Callable (a) #实例a不可调用False >>> a () #调用实例a失败Traceback (most recent):  File "<pyshell#31>", Line 1, in <module>    A () TypeError: ' A ' object was not callable>>> class B: #定义类B    def __call__ (self): C4/>print (' instances is callable now. ')        >>> callable (b) #类B是可调用对象True >>> B = B () #调用类B >>> callable (b) #实例b是可调用对象True >>> B () #调用实例b成功instances is callable now.

Python built-in function (--callable)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.