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 .
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 .
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 .
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]
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 .
Python A tuple of is similar t