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

How to understand Python built-in functions

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "how to understand Python built-in functions". In the operation of practical cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

These diagrams are taken from python.org and list the built-in functions in python3.10.

But are these really all functions?

Let's test it:

Import typesimport inspectlst = dir (_ _ builtins__) for name in lst: print (name, eval (f'type ({name})'))

All lst gets is the name of the content in the _ _ builtins__ module, which means that lst is a list of strings. But what each string represents can have its own meaning. For example, the "sum" string represents a built-in function, and the "str" string represents a built-in class.

By executing the type function in the way of eval, we can get the type object description corresponding to each name, for example:

Bool

In fact, we can find that, like type, dict,str,range,list,tuple,zip is not a function name but a type name at all:

Dict

List

Map

Range

Set

Str

Tuple

Type

Zip

So, words like range (10) and type ("c") call the constructor to create an instance of the corresponding type.

Next, let's dig a step further and output all the built-in functions and built-in types in _ _ builtins__, respectively.

Output built-in function:

Import typesimport inspectlst = dir (_ _ builtins__) for name in lst: if eval (f'type ({name})') is types.BuiltinFunctionType: print (f'{name} is a built-in function') if inspect.isbuiltin (getattr (_ _ builtins__, name)): print (f'{name} is a built-in function') fs = inspect.getmembers (_ _ builtins__, inspect.isbuiltin) print (fs)

There are three ways to do this:

1. If its type object is types.BuiltinFunctionType, this is a built-in function.

2. If everything is an object in Python, getattr (_ _ builtins__,name) regards the _ _ builtins__ module as an object and obtains the specific content corresponding to the name name. This is actually the reflection in Python. After obtaining the specific content, use the isbuitin function of inspect to detect whether it is a built-in function. (note: the isfunction function of the inspect module can only be used to detect ordinary functions, but the built-in functions are not ordinary functions, so isfunction detects built-in functions to get False. To use the isbuiltin function for detection. )

3. Since _ _ builtins__ is an object, you can use inspect's getmemebers function and add a filter inspect.isbuiltin to filter out the built-in functions in _ _ builtins__ directly.

Next is the output of the built-in class:

Import typesimport inspectlst = dir (_ _ builtins__) for name in lst: print (name, eval (f'type ({name})') for name in lst: if inspect.isclass (getattr (_ _ builtins__, name)): print (f'{name} is a built-in class') cs = inspect.getmembers (_ _ builtins__, inspect.isclass) print (cs)

Because there is no such detection for classes in types. So here is the use of inspect.isclass for detection.

In other words, built-in classes are also classes.

That's all for "how to understand Python built-in functions". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Development

Wechat

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

12
Report