Home > Article > Backend Development > Understand why Python's built-in functions aren't everything?
python video tutorial column introduces you to Python built-in functions.
In the previous article of Python Cat
, we compared two methods of creating lists, namely literal usage[] Use list() with built-in types to analyze the difference in their running speed.
When analyzing why list() is slower, the article mentioned that it requires two steps: name search and function calling. Then, this leads to a new question: list() Isn't it a built-in type? Why can't it directly call the logic of creating a list? That is, why does the interpreter have to go through a name lookup to "know" what to do?
In fact, the reason is very simple: the names of built-in functions/built-in types are not keywords. They are just a convenient function built into the interpreter for developers to use out of the box.
PS: Built-in function is very similar to built-in type, but list() is actually a built-in type rather than a built-in function. I have done an analysis of these two confusing concepts, please see this article. In order to facilitate understanding and expression, they are collectively referred to as built-in functions below.
The names of built-in functions are not keywords, and they can be reassigned.
For example, the following example:
# 正常调用内置函数list(range(3)) # 结果:[0, 1, 2]# 定义任意函数,然后赋值给 listdef test(n): print("Hello World!") list = test list(range(3)) # 结果:Hello World!复制代码
In this example, we assigned the custom test to the list, and the program did not report an error. . This example can even be changed to directly define a new function with the same name, that is, "def list(): ...".
This shows that list is not a restricted keyword/reserved word in Python.
Looking at the official documentation, you can find that Python 3.9 has 35 keywords, the details are as follows:
If we assign the test in the above example to any key words, such as "pass=test", an error will be reported: SyntaxError: invalid syntax.
From this, we can see from this perspective that built-in functions are not omnipotent: Their names are not as stable as keywords, although they are in the built-in scope of the system. But it can be easily intercepted by objects in the user's local scope!
Because the order in which the interpreter looks for names is "local scope -> global scope -> built-in scope", built-in functions are actually at the lowest priority.
For novices, unexpected situations may occur (there are 69 built-in functions, and it is difficult to remember them all).
So, Why doesn’t Python set the names of all built-in functions to non-overridable keywords?
On the one hand, the reason is that it wants to control the number of keywords, and on the other hand, it may want to leave more freedom to users. Built-in functions are only recommended implementations of the interpreter. Developers can implement functions with the same name as the built-in functions according to their needs.
However, such scenarios are rare, and developers usually define functions with different names. Taking the Python standard library as an example, the ast
module has the literal_eval() function (standard eval () built-in function), the pprint
module has the pprint() function (compared to the print() built-in function), and the itertools
module has the zip_longest() function (compared to the zip() built-in function) function)...
Because the name of the built-in function is not a reserved keyword, and it is at the end of the name search sequence, the built-in function has Probably not the fastest.
The previous article showed the fact that [] is 2~3 times faster than list(). In fact, this can also be extended to str(), tuple(), set( ), dict() and other built-in types, literal usage is slightly faster than built-in type usage.
For these built-in types, when we call xxx(), it can be simply understood that the class is being instantiated. In object-oriented languages, it is normal for classes to be instantiated first and then used.
However, this approach sometimes seems cumbersome. For ease of use, Python provides literal representations for some commonly used built-in types, namely "", [], (), {}, etc., which represent data types such as strings, lists, tuples, and dictionaries. .
Document source: docs.python.org/3/reference…
Generally speaking, all programming languages must have some literal representation, but they are basically limited to basic types such as numeric types, strings, Boolean types, and null.
Python has also added literals for several data structure types, so it is more convenient. This also explains why the built-in functions may not be the fastest.
Generally speaking, with the same complete functions, built-in functions are always faster than our custom functions, because the interpreter can do some underlying optimizations, such as len() built-in functions are definitely faster than user-defined x The .len() function is fast.
Some people have formed the misunderstanding that "built-in functions are always faster" based on this.
Compared with user-defined functions, the built-in functions of the interpreter are close to the back door; while the literal representation is a faster back door compared to the built-in functions.
In other words, some built-in functions/built-in types are not the fastest when there is literal representation!
It is true that Python itself is not omnipotent, and any of its grammatical components (built-in functions/types) are not omnipotent. However, generally we think that built-in functions/types are always "superior", receive many special preferential treatment, and appear to be "omnipotent".
This article solves the problem from "list() actually loses to []" and reveals that there are actually some shortcomings of built-in functions from two perspectives: The name of the built-in function is not a keyword, but the built-in function Scope is at the lowest priority for name lookups, so some built-in functions/types will execute significantly slower than their corresponding literal representations when called.
This article extends the discussion on the previous topic "Why Python". On the one hand, it enriches the previous content. On the other hand, it also helps everyone understand several basic concepts of Python and its implementation.
If you like this article, please like it and support it! In addition, I have also written 20 similar topics, please follow Python猫
to view it, and give me a little star on Github~~
Related Free learning recommendations: python video tutorial
The above is the detailed content of Understand why Python's built-in functions aren't everything?. For more information, please follow other related articles on the PHP Chinese website!