Get the App
SLTechnology News&Howtos  ›  Development  › 

How to make python code more Pythonic

Shulou Source: shulou.com Published: 2022-06-02 23:14:26 10月02日 Update

Today, I will talk to you about how to make the python code more Pythonic. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

This article, mainly introduces a few simple techniques, let you write Python code, more Pythonic.

Variable exchange

Pythonic writing method

Common writing method

Loop through interval elements

In Python2, there are range and xrange2 writing methods, and xrange is a generator writing method, which saves memory. Range in Python3 is equivalent to xrange in Python2.

The generator is generated dynamically only when it is used, and can only be used once, such as range (1000000). A list of 1 million elements is generated in Python2, but not a list, but a generator in Python3, which takes up very little memory.

If you are still using Python2, it is recommended to use xrange instead of range

The way Java is written

Generator extension

Indexes

Pythonic writing method

Common writing method

Obviously, Pythonic is written in a more intuitive and elegant way.

String concatenation

Pythonic writing method

Common writing method

Each + operation produces a new string, resulting in a waste of memory, while join produces only one string object in the whole process.

File opening and closing

Pythonic writing method

Common writing method

Using with,Python will automatically manage the opening and closing of file streams without manual operation

List operation

Pythonic writing method

Common writing method

List can also delete * elements with pop (0), but list is stored sequentially in memory. Deleting * elements will cause all subsequent elements to move forward, which is inefficient and similar to insertion.

Avoid using list if there are a large number of deletions and inserts at the beginning

Deconstruction assignment

Pythonic writing method

Similarly, in Python2, the dictionary's items method returns a list, which consumes a lot of memory when the dictionary is large. The iteritems method returns the generator.

In Python3, no iteritems,items is equivalent to the iteritems of Python2.

If you are using Python2, use iteritems instead of items

Derivation type

Pythonic writing method

Common writing method

After reading the above, do you have any further understanding of how to make python code more Pythonic? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Tags: Writing generation normal memory elements generators code content dictionaries characters strings files methods equivalence articles elegant intuitive dynamic intervals variables Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Shulou Information NVidia MariaDB Microsoft