Table of Contents
1. The search priority of built-in functions is the lowest
2. The built-in function may not be the fastest
Summary
Home Backend Development Python Tutorial Understand why Python's built-in functions aren't everything?

Understand why Python's built-in functions aren't everything?

Oct 21, 2020 pm 05:12 PM
python built-in functions

python video tutorial column introduces you to Python built-in functions.

Understand why Python's built-in functions aren't everything?

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.

1. The search priority of built-in functions is the lowest

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!复制代码
Copy after login

Understand why Python's built-in functions aren't everything?

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:

Understand why Python's built-in functions aren't everything?

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)...

2. The built-in function may not be the fastest

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.

Understand why Python's built-in functions aren't everything?

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. .

Understand why Python's built-in functions aren't everything?

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!

Summary

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

Is the vscode extension malicious? Is the vscode extension malicious? Apr 15, 2025 pm 07:57 PM

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

How to run python with notepad How to run python with notepad Apr 16, 2025 pm 07:33 PM

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

See all articles