This article is from the “Why Python” series

In the last Python Cat article, we compared two ways of creating lists, the literal [] and the built-in list(), to see how fast they run.

List () is a built-in type, so why can’t it just call the logic that creates the list? That is, why does the interpreter have to go through a name lookup to “know” what to do?

The reason is simple: the names of built-in functions/built-in types are not keywords, they are just a convenience built into the interpreter for developers to use out of the box.

Built-in function is similar to the built-in type built-in type, but list() is actually a built-in type rather than a built-in function. Check out this article where I’ve made a distinction between these two confusing concepts. For ease of understanding and presentation, the following are collectively referred to as built-in functions.

1. Built-in functions have the lowest search priority

The names of built-in functions are not keywords; they can be reassigned.

Take this example:

Call the built-in function normally
list(range(3))  [0, 1, 2]

Define any function and assign it to list
def test(n) :
    print("Hello World!")
list = test
list(range(3)) # Result: Hello World!
Copy the code

In this example, we assign a custom test to a list, and the program does not report an error. This example could even be changed to simply define a new function of the same name: “def list():…” .

This shows that list is not a Python-qualified keyword/reserved word.

Python 3.9 has 35 keywords, as described below:

If we assign test to any keyword, such as “pass=test”, we will get SyntaxError: invalid syntax.

From this point of view, we can see that built-in functions are not panacea: their names are not as solid as keywords, and although they are in the system’s built-in scope, they can be easily intercepted by user-scoped objects!

Because the interpreter looks for names in the order “local -> global -> built-in”, built-in functions are in the lowest priority.

For starters, there’s a certain amount of potential for surprises (there are 69 built-in functions, and it’s hard to remember them all).

So why doesn’t Python make the names of all built-in functions unrepeatable keywords?

Partly because it wants to control the number of keywords, and partly because it wants to give users more freedom. Built-in functions are simply recommended implementations of the interpreter, and developers can implement functions with the same name as the built-in functions as needed.

However, such scenarios are rare, and developers generally define functions with different names, such as the Python standard library, The AST module has literal_eval() (built-in eval()), the pprint module has pprint(built-in), and the Itertools module has zip_longest() Zip () built-in function)……

2. Built-in functions may not be the fastest

Because the name of the built-in function is not a reserved keyword and it is at the bottom of the name lookup order, the built-in function may not be the fastest.

The previous article showed that [] is two or three times faster than list(), but this extends to STR (), tuple(), set(), dict(), and other built-in types, all of which are slightly faster for literals than for built-in types.

For these built-in types, when we call XXX (), we can simply understand that we are instantiating the class. In object-oriented languages, it is perfectly normal for classes to be instantiated before they are used.

However, this approach is sometimes cumbersome. For ease of use, Python provides literal representations for some commonly used built-in types, namely “”, [], (), {}, and so on, representing data types such as strings, lists, tuples, and dictionaries.

The document reference: docs.python.org/3/reference…

In general, all programming languages must have some literal representation, but they are mostly limited to numeric types, strings, Booleans, and basic types like NULL.

Several types of data structure literals have been added to Python, so they are more convenient, and this explains why built-in functions may not be the fastest.

In general, the built-in function is always faster than our custom function for the same complete function, because the interpreter can do some low-level optimizations, such as len() built-in function is definitely faster than the user-defined x.len() function.

This leads some people to the misconception that built-in functions are always faster.

Compared with user-defined functions, interpreter built-in functions are close to the back door. Literal representations are a faster backdoor to built-in functions.

That is, some built-in functions/built-in types are not the fastest in the case of literal representations!

summary

To be sure, Python itself is not a panacea, and neither are any of its syntactic components (built-in functions/types). However, we tend to think of built-in functions/types as being “superior”, given a lot of special privileges, as being “all-powerful”.

This article from “list () would be lost to []” presentation, amplification from two angles to reveal the built-in function actually exists some deficiencies: built-in function name is not a keyword, and built-in scope in the name lookup the lowest priority, therefore in the call, some built-in function/type has a significantly slower than the speed of execution of their corresponding literal notation.

This article is a continuation of the previous “Why Python” topic, which enriches the previous content and helps you understand some of the basic concepts of Python and its implementation.

If you liked this post, please give it a thumbs up! In addition, I have written 20+ articles on similar topics, please follow Python cat and give me a star on Github

–>>> Finally, the welfare moment:

I have compiled more than 100 excellent articles from two years of writing into a 700-page elegant Python ebook, which I recommend!! Please follow Python Cat on wechat and reply “elegant” to get ~~