Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the hidden eggs in Python?

Shulou Source: shulou.com Published: 2022-06-02 01:55:16 11月16日 Update

This article introduces the relevant knowledge of "what are the hidden eggs in Python". In the operation of actual 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!

1. Use re.DEBUG to view the matching process of regular expressions

Regular expressions are a feature of Python, but it can be painful to debug and it's easy to get a bug. Fortunately, Python can print out a parsing tree of regular expressions and use re.debug to show the complete process of re.compile.

Once you understand the grammar, you can find your mistakes. Here we can see that [/ font] forgot to remove []

2. The enumerate function is used to traverse the elements in the list and their subscripts

3. Be more careful with default arguments

Instead, you should use a tag value for "undefined" instead of "[]".

4. For C developers who prefer parentheses to indentation, you only need to use the following command:

From _ _ future__ import braces

5. Tricks in slicing operation

The special example is x [::-1], which can reverse the list > > a [::-1] [5] [:: 2] [1]

6. Decorator

The decorator implements calling other functions or methods in a function to increase functionality, thus modifying parameters or results, etc., adding a decorator before the function definition, with only an "@" symbol.

The following example shows the use of a print_args decorator:

7. Trick for fetching parameters

You can use * or * * to fetch a list or dictionary as a function argument

8. Exception else statement

Using "else" is better than adding extra code to "try" statements, because it avoids accidentally getting exceptions that are not protected by try statements. Except for the statement.

9. Nested list derivation and generator expressions

[(iMagnej) for i in range (3) for j in range (I)]

((iMagnej) for i in range (4) for j in range (I)

These statements can replace a large number of nested loop code blocks

10. Main sentence patterns

Import this

Let's read the essentials of Python Zen again (The Zen of Python, by Tim Peters):

Beautiful is better than ugly.

Explicit is better than implicit.

Simple is better than complex.

Complex is better than complicated.

Flat is better than nested.

Sparse is better than dense.

This is the end of the content of "what are the Hidden Egg in Python"? 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!

Tags: Functions expressions statements rules procedures eggs code content parameters just more knowledge practical special lucky unexpected painful learned next subscript Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Xiaomi Docker Apple NVidia