Python built-in functions (9)--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.

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.

>>>classA:#Defining Class A    Pass>>> Callable (A)#Class A is a callable objectTrue>>> a = A ()#Call Class A>>> Callable (a)#instance A is not callableFalse>>> A ()#failed to invoke instance aTraceback (most recent): File"<pyshell#31>", Line 1,inch<module>A () TypeError:'A'Object is  notcallable>>>classB:#Defining Class B    def __call__(self):Print('instances is callable now.')        >>> Callable (B)#Class B is a callable objectTrue>>> B = B ()#Call Class B>>> Callable (b)#Instance B is a callable objectTrue>>> B ()#Invoking instance B succeedsInstances is callable now.

Python built-in functions (9)--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.