Python built-in functions (9) -- callable, pythoncallable

Source: Internet
Author: User

Python built-in functions (9) -- callable, pythoncallable

English document:

callable(Object)

ReturnTrueIfObjectArgument appears callable,FalseIf not. If this returns true, it is still possible that a call fails, but if it is false, callingObjectWill never succeed. Note that classes are callable (calling a class returns a new instance); instances are callable if their class has__call__()Method.

Note:

1. The method is used to check whether an object can be called. The call indicates whether the object can be called using the () method.

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

2. callable objects may also fail to be called in actual cases. However, if an object cannot be called, the call will certainly fail.

3. All class objects are callable objects. Whether the class object can be called depends on whether the class defines the _ call _ method.

>>> Class A: # define class A pass >>> callable (A) # class A is a callable object True >>> A = () # call Class A >>> callable (a) # instance a cannot call False >>> a () # call instance a failed Traceback (most recent call last ): file "<pyshell #31>", line 1, in <module> a () TypeError: 'A' object is not callable >>> class B: # define class B def _ call _ (self): print ('instances' are callable now. ') >>> callable (B) # Class B is the callable object True >>> B = B () # Call Class B >>> callable (B) # instance B is the callable object True> B () # instances are callable now.

 

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.