程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Three important concepts to improve Python coding ability

編輯:Python

1. introduction

Python By Guido Van Rossum On 1991 A programming language created in . In the past few years , More and more companies are using Python Project development , Mainly because of its simple syntax , There are many easy-to-use third-party libraries . This paper mainly focuses on Python Abstract summary of some concepts in , Understanding and using them can greatly improve everyone's coding ability .

2. Context manager

The context manager allows us to allocate and release the resources of the context in the best way . For the management of some resources , If not handled properly , There may be some very strange phenomena , It's confusing . The context manager can ensure that resources are released normally after use .
Generally speaking , We mainly use with Keyword to use it . The most common case of using context manager is to manipulate files . After the operation on the file , It needs to be turned off correctly , The context manager can easily complete the corresponding operations for us by skipping specific details , Examples are as follows :

with open('myfile.xtx', 'r') as f:
content = f.read()

Look at the code above , We don't show calls f.close() Method . The context manager will automatically handle the file closing operation for us .

3. Type tips

Type hints can help us write clean 、 Interpretable code . The way to apply it is “ Indicate the ” The type of the parameter and the return value of the function . for example , The text entered by the user is always an integer . So , We wrote a function , This function returns... According to our verification True or False:

def validate_integer(user_input):
...

Now that we know what this function does , Then it's easy to understand by looking at the definition . however , Without the above description , Just look at the declaration of the above function , It's not so easy to understand . user_input What is the type of the parameter ? Where did it come from ? Is it already an integer ? By refactoring the code into the following form , We can answer these questions through statements :

def validate_integer(user_input: str) -> bool:
...

Let's look at the declaration of the above function , It's very easy to explain , Even people who read this code for the first time .

4. Shallow copy and deep copy

For new developers , This is a concept that is often mistaken . Let's take an example , Suppose we create a list a, Then assign this list to a new variable b:

>>> a = [1, 2, 3]
>>> b = a

next , Let's try on the list b Insert a new value in , Then print two lists :

>>> b.append(4)
>>> print(b)
[1, 2, 3, 4]
>>> print(a)
[1, 2, 3, 4]

Many people will find it strange , Because the new value has been inserted into two lists ! This happens mainly because in Python When assigning a list in , Unless otherwise stated , Otherwise, the list will not be copied . Above list b It's just a list a References to , Is a shallow copy . The illustration of the above example is explained as follows :


The above legend means that the operations in the two variables will be reflected in the same list . If we do generate a list accordingly a Copy of , At this point, we need to use deep copy , That is to use .copy() Methods to perform related operations :

>>> a = [1, 2, 3]
>>> b = a.copy()
>>> b.append(4)
>>> print(b)
[1, 2, 3, 4]
>>> print(a)
[1, 2, 3]

5. summary

This article focuses on some things that can be improved Python The concept and personal insight of developers' coding ability , I hope you can learn relevant skills from it . Of course , Like any programming language , I suggest you do more , Practice makes perfect .


Official account 《AI The way of algorithm 》, For more AI Algorithm information .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved