This article describes how to write efficient and elegant Python code and how to compile python.

Source: Internet
Author: User

This article describes how to write efficient and elegant Python code and how to compile python.

This article is partially extracted from books: Effective Python & Python3 Cookbook, but it has also been modified and added the author's own understanding and Best Practices in application.

The full text is about 9956 words. It may take 24 minutes to finish reading.

Pythonic list Cutting

list[start:end:step]

If you start cutting from the beginning of the list, ignore the 0 value of start, for examplelist[:4]

If it is always switched to the end of the list, the end 0 is ignored. For example:list[3:]

During list cutting, even if the start or end indexes do not have problems across the border

List slices do not change the original list. If the index is left blank, a copy of the original list is generated.

List Derivation

Use List derivation to replacemapAndfilter

Do not use a list derivation statement containing more than two expressions

When there is a large amount of data, the list derivation may consume a large amount of memory. We recommend that you use a generator expression.

Iteration

Use to obtain the indexenumerate

enumerateThe second parameter can be accepted, which is added as an iterationindexValue on

UsezipTraverse two iterators at the same time

zipReturns a tuple of data.

AboutforAndwhileAfter the loopelseBlock

LoopNormal endWill be called laterelseInternal code

ThroughbreakSkip the loop, and the execution will notelse

If the sequence to be traversed is empty, execute immediatelyelse

Reverse Iteration

For common sequences (lists), we can use the built-inreversed()Function Iteration:

In addition, you can also__reversed__Method To perform reverse iteration of the class:

try/except/else/finally

IftryIf no exception occurs inelseInternal code

elseThefinallyRun earlier

Will be executed eventuallyfinally, Which can be cleaned up

Function use decorator

The modifier is used to modify an existing function without changing the original function code. A common scenario is to add a debugging statement or add an existing function.logMonitoring

Example:

In addition, you can also compile the decorator that receives parameters. In fact, a function is nested in the outer layer of the original decorator:

However, there is a problem when using the decorator like above:

That is to say, the original function has beennew_funThe function is replaced. Calling a decorated function is equivalent to calling a new function. When viewing the parameters, comments, and even function names of the original function, you can only view information about the decorator. To solve this problem, we can use

Python built-infunctools.wrapsMethod.

functools.wrapsIt is a very hack method. It can be used as a decorator and is used as a function to be returned inside the decorator. That is to say, it is the decorator of the decorator and takes the original function as a parameter to reserve the information of the original function, so that when we view the information of the decorated original function, it can be the same as the original function.

In addition, sometimes we may do more than one thing in the decorator. At this time, we should separate the event as an additional function. However, because it may only be related to the decorator, You can construct a decorator class. The principle is very simple, mainly writing__call__Method, so that the class can be called like a function.

Use Generator

Consider using the generator to rewrite the function that directly returns the list.

There are several minor issues with this method:

Call each time a qualified result is obtainedappendMethod. But in fact, our focus is not on this method at all. It is just a means for us to achieve our goals. In fact, we only needindexOkay.

ReturnedresultContinue Optimization

Data existsresultIf the data volume is large, the memory usage will be high.

Therefore, use the generatorgeneratorBetter. Generator is usedyieldExpression Function. When the generator is called, it does not actually execute, but returns an iterator. Each time it calls the built-innextFunction, the iterator will push the generator to the nextyieldExpression:

After obtaining a generator, You can traverse it normally:

If you still need a list, you can use the function call result as a parameter before callinglistMethod

Iteratable object

It should be noted that a common iterator can only iterate for one round, and repeated calls after one round are invalid. To solve this problem, you canDefine an iteratable container class:

In this case, it is okay to repeat the number of iterations of the class instance:

However, you must note that only the implementation__iter__Method iterator, only throughforLoop To iterate; to passnextMethod iteration requiresiterMethod:

Use location parameters

Sometimes, the number of parameters received by a method may not be certain. For example, to define a summation method, at least two parameters must be received:

If the number of parameters received is not certain, and the parameter input sequence is not concerned, the location parameter should be used.*args:

Note that parameters with an indefinite lengthargsWhen passed to a function, you must first convert it into a metagroup.tuple. This means that if you bring a generator as a parameter to the function, the generator will traverse it and convert it to a tuple. This may consume a large amount of memory:

Use keyword Parameters

Keyword parameters can improve code readability

You can use keyword parameters to provide default values for functions.

Extended Function Parameters

Define functions that can only use keyword Parameters

In normal mode, keyword parameters are not required

UsePython3Method to force keyword parameters in

UsePython2Method to force keyword parameters in

Default values of parameters

It's a common story:The default value of the function is set only once when the program loads the module and reads the definition of the function.

That is to say, if a parameter is assigned a dynamic value (

For example[]Or{}), If other parameters are assigned to the parameter when the function is called later, the default value defined previously will change when the function is called later, the value assigned to the last call:

Therefore, we recommend that you useNoneAs the default parameter, values are assigned after being determined in the function:

Class__slots__

By default, Python uses a dictionary to save the instance attributes of an object. This allows us to dynamically add new attributes to the class instance during running:

However, this dictionary wastes extra space. In many cases, we will not create so many attributes. Therefore__slots__You can tell Python

Instead of using dictionaries, you can use a fixed set to allocate space.

__call__

By defining__call__Method, so that instances of this class can be called as normal functions.

The benefit of this method is that the State can be saved through the class attributes without creating a closure or global variable.

@classmethod&@staticmethod

@classmethodAnd@staticmethodBut their application scenarios are not the same.

The common methods inside the class areselfAs the first parameter, it indicates that when an instance is called, the scope of the instance is passed into the method;

@classmethodToclsAs the first parameter, it indicates passing in the scope of the class itself. The first parameter passed in by default is the class itself, no matter whether it is called through a class or through an instance of the class.

@staticmethodYou do not need to input the default parameter, which is similar to a common function.

To understand their application scenarios through instances:

Suppose we need to createDateUsed to store three data records per year, month, or day.

The above Code createsDateClass, which will be set during initializationday/month/yearAndpropertySetgetter, You can passtimeObtain the storage time:

But what if we want to change the attribute input method? After all, it is annoying to input the year, month, or day attributes during initialization. Can you find a method that can be passed in without changing the existing interfaces and Methods2016-11-09Create a stringDateInstance?

You may think of the following method:

But not good enough:

An extra method is written out of the class, and parameters must be obtained after each formatting.

This method only followsDateClass-related

The problem of too many input parameters is not solved.

You can use@classmethod, Create a format string inside the class and return the method of the class instance:

In this way, we can useDateClass to callfrom_stringMethod To create an instance without aggression, and modify the old instantiation method:

Benefits:

In@classmethod, You can useclsParameter to obtain the same convenience as the external call class.

This method can be further encapsulated to improve reusability.

More in line with Object-Oriented Programming

While@staticmethodBecause it is similar to a common function, you can take the helper related to this class

Method@staticmethod, Put it in the class, and then call this method directly through the class.

Use the date-related helper functions@staticmethodMethod placementDateClass, you can call these methods through the class:

Create a context Manager

The context manager is generally used to prepare the code block before it is executed. After the code block is executed, it completes the final processing.withStatements are often accompanied by the context manager. Typical scenarios include:

PasswithStatement, the Code completes the file opening operation, and closes the file automatically when the call ends, or when an exception occurs, that is, the processing after the file is read and written. If the context manager is not used, the Code is as follows:

Is it complicated? Therefore, the advantage of using the context manager is that by calling the pre-configured callback, it automatically helps us to process the work when the code block is executed and completed. However__enter__And__exit__Method, we can customize a context manager.

Then you can call it in this way:

When calling:

withThe statement is savedReadFileClass__exit__Method

Then callReadFileClass__enter__Method

__enter__Method to open the file and return the resultwithStatement

The result of the previous step is passedfile_readParameters

InwithStatementfile_readParameters to read each row

After reading,withThe__exit__Method

__exit__Method to close the file

Note that__exit__Method, we close the file, but finally returnTrue, So the error will not bewithStatement throw. OtherwisewithStatement will throw a corresponding error.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.