Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of callable () in python

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about the role of callable () in python. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

Callable syntax: callable (object)

Returns True if the parameter object passed to callable is callable, and vice versa. If callable (obj) returns True, it does not necessarily mean that obj is callable. If False is returned, it is definitely not a callable object.

Extension: interactive command line entry dir (locals () ["builtins"]) to view the built-in

Case 1 # callalbe (obj)

K = 5

Print (callable (k)) # False

Def testFuction ():

Print ("Test")

Y = testFuction # variable y points to the function name memory address

Print (callable (y)) # True

The above k is not callable (that is, not callable), testFuction is callable, you can see that the integer data is not callable, the function is callable.

Case 2 can be called: class Foo:

Def _ call__ (self):

Print ("Print something")

Print ("Foo", callable (Foo)) # True

The instance object of the class can be called: class Foo:

Def _ call__ (self):

Print ("Print something")

InstanceofFoo = Foo () # instantiate object

InstanceofFoo () # Print something instance is followed by parentheses to indicate calling the _ _ call__ () function

Case 3: although callable returns True to show callable, it is not a callable object: class Foo:

Def printLine (self):

Print ('test')

Print (callable (Foo)) # True

Class is callable, but no call () method is defined, then the instance cannot be called: class Foo:

Def printLine (self):

Print ('test')

Print (callable (Foo)) # True

InstanceofFoo = Foo ()

Print (callable (instanceofFoo)) # False

InstanceofFoo () # instantiate call call # TypeError: 'Foo' object is not callable

After reading the above, do you have any further understanding of the role of callable () in python? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report