Python callable () function usage instance analysis, pythoncallable

Source: Internet
Author: User

Python callable () function usage instance analysis, pythoncallable

This example describes the usage of the Python callable () function. We will share this with you for your reference. The details are as follows:

Built-in functions in pythoncallable( ), YesCheck whether an object is callable.

For functions, methods, lambda functions, classes, and Implementations_ _call_ _Method class instance, which returns True.

>>> help(callable)Help on built-in function callable in module __builtin__:callable(...) callable(object) -> bool Return whether the object is callable (i.e., some kind of function). Note that classes are callable, as are instances with a __call__() method.

1. functions are callable.

>>> def add(x,y):...  return x+y...>>> callable(add)True

2. Class and Class methods are callable

>>> Class C:... def printf (self):... print 'This is class C! '... >>> ObjC = C () >>> callable (C) # classes are callable. When you call them, a corresponding class instance is generated. true >>> callable (C. printf) True >>> callable (objC. printf) True

3.__call__()Method class instances are callable

>>> Class A:... def printf (self):... print 'This is class! '... >>> ObjA = A () >>> callable (A) # classes are callable. When you call them, A corresponding class instance is generated. true >>> callable (objA) # Class A does not implement the _ call _ () method. Therefore, an instance of class A is not callable, False >>> class B :... def _ call _ (self ):... print 'this is class B! '... >>> ObjB = B () >>> callable (B) # classes are callable. When you call them, a corresponding class instance is generated. true >>> callable (objB) # Class B implements the _ call _ () method. Therefore, Class B instances are callable to True.

4. lambda expressions are callable

>>> f=lambda x,y:x+y>>> f(2,3)5>>> callable(f)True

Others, such as integers, strings, lists, tuples, and dictionaries, are not callable, as shown below:

>>> callable(2)False>>> callable('python')False>>> l=[1,2,3]>>> callable(l)False>>> t=(4,5,6)>>> callable(t)False>>> d={'a':1,'b':2}>>> callable(d)False

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.