


Detailed explanation of Python traps and precautions commonly encountered in development
I have encountered some pitfalls recently when using Python, such as using the datetime.datetime.now() variable objectAs the default parameters of functions , modules loops dependencies, etc.
Record it here for future query and supplement.
Avoid mutable objects as default parameters
In the process of using functions, default parameters are often involved. In Python, when using mutable objects as default parameters, unexpected results may occur.
Let’s look at an example:
def append_item(a = 1, b = []): b.append(a) print b append_item(a=1) append_item(a=3) append_item(a=5)
The result is:
[1] [1, 3] [1, 3, 5]
As you can see from the result, when the append_item function is called twice later, the function parameters b is not initialized to [], but retains the value of the previous function call.
The reason why this result is obtained is that in Python, the default value of a function parameter is only initialized once when the function is defined.
Let’s look at an example below to prove this feature of Python:
class Test(object): def init(self): print("Init Test") def arg_init(a, b = Test()): print(a) arg_init(1) arg_init(3) arg_init(5)
The result is:
Init Test 1 3 5
As you can see from the results of this example, the Test class has only been instantiated. Once, that is to say The default parameters have nothing to do with the number of function calls and are only initialized once when the function is defined.
Correct use of variable default parameters
For variable default parameters, we can use the following pattern to avoid the above unexpected results:
def append_item(a = 1, b = None): if b is None: b = [] b.append(a) print b append_item(a=1) append_item(a=3) append_item(a=5)
The result is:
[1] [3] [5]
Scope in Python
The scope resolution order of Python is Local, Enclosing, Global, Built-in, which means that the Python interpreter will parse according to this ordervariable.
Look at a simple example:
global_var = 0 def outer_func(): outer_var = 1 def inner_func(): inner_var = 2 print "global_var is :", global_var print "outer_var is :", outer_var print "inner_var is :", inner_var inner_func() outer_func()
The result is:
global_var is : 0 outer_var is : 1 inner_var is : 2
In Python, there is one thing to note about scope. Yes, when assigning a value to a variable in a scope, Python will consider the variable to be a local variable of the current scope.
It is relatively easy to understand this point. For the following code, var_func assigns a value to the num variable, so num here is a local variable in the var_func scope.
num = 0 def var_func(): num = 1 print "num is :", num var_func()
Question 1
However, when we use variables in the following way, problems will arise:
num = 0 def var_func(): print "num is :", num num = 1 var_func()
The results are as follows:
UnboundLocalError: local variable 'num' referenced before assignment
The reason why this error occurs is because we assigned a value to the num variable in var_func, so the Python interpreter will think that num is a local variable in the var_func scope, but when the code is executed to print "num is:", the num statement At this time, num is still undefined.
Question 2
The above error is relatively obvious, and there is also a more subtle error form as follows:
li = [1, 2, 3] def foo(): li.append(4) print li foo() def bar(): li +=[5] print li bar()
code The result is:
[1, 2, 3, 4] UnboundLocalError: local variable 'li' referenced before assignment
In the foo function, according to Python's scope parsing order, the global li variable is used in this function; but in the bar function, the li variable is assigned a value, so li will be Treated as a variable in bar scope.
For this problem of the bar function, you can use the global keyword.
li = [1, 2, 3] def foo(): li.append(4) print li foo() def bar(): global li li +=[5] print li bar()
ClassAttributeHide
In Python, there are class attributes and instance attributes. Class attributes belong to the class itself and are shared by all class instances.
Class attributes can be accessed and modified through the class name or through the class instance. However, when an instance defines an attribute with the same name as the class, the class attribute is hidden.
Look at the following example:
class Student(object): books = ["Python", "JavaScript", "CSS"] def init(self, name, age): self.name = name self.age = age pass wilber = Student("Wilber", 27) print "%s is %d years old" %(wilber.name, wilber.age) print Student.books print wilber.books wilber.books = ["HTML", "AngularJS"] print Student.books print wilber.books del wilber.books print Student.books print wilber.books
The result of the code is as follows. At first, the wilber instance can directly access the books attribute of the class, but when the instance wilber After defining the instance attribute named books, the books attribute of the wilber instance "hides" the books attribute of the class; when delete the books attribute of the wilber instance, wilber.books corresponds to the books of the class attribute.
Wilber is 27 years old ['Python', 'JavaScript', 'CSS'] ['Python', 'JavaScript', 'CSS'] ['Python', 'JavaScript', 'CSS'] ['HTML', 'AngularJS'] ['Python', 'JavaScript', 'CSS'] ['Python', 'JavaScript', 'CSS']
When using inheritance in Python values, you should also pay attention to the hiding of class attributes. For a class, you can view all class attributes through the dict attribute of the class.
When accessing a class attribute through the class name, the dict attribute of the class will be searched first. If the class attribute is not found, the parent class will continue to be searched. However, if a subclass defines a class attribute with the same name as the parent class, the class attributes of the subclass will hide the class attributes of the parent class.
Look at an example:
class A(object): count = 1 class B(A): pass class C(A): pass print A.count, B.count, C.count B.count = 2 print A.count, B.count, C.count A.count = 3 print A.count, B.count, C.count print B.dict print C.dict
The results are as follows. When class B defines the count attribute, the count attribute of the parent class will be hidden:
1 1 1 1 2 1 3 2 3 {'count': 2, 'module': 'main', 'doc': None} {'module': 'main', 'doc': None}
tuple是“可变的”
在Python中,tuple是不可变对象,但是这里的不可变指的是tuple这个容器总的元素不可变(确切的说是元素的id),但是元素的值是可以改变的。
tpl = (1, 2, 3, [4, 5, 6]) print id(tpl) print id(tpl[3]) tpl[3].extend([7, 8]) print tpl print id(tpl) print id(tpl[3])
代码结果如下,对于tpl对象,它的每个元素都是不可变的,但是tpl[3]是一个list对象。也就是说,对于这个tpl对象,id(tpl[3])是不可变的,但是tpl[3]确是可变的。
36764576 38639896 (1, 2, 3, [4, 5, 6, 7, 8]) 36764576 38639896
Python的深浅拷贝
在对Python对象进行赋值的操作中,一定要注意对象的深浅拷贝,一不小心就可能踩坑了。
当使用下面的操作的时候,会产生浅拷贝的效果:
使用切片[:]操作
使用copy模块中的copy()函数
使用copy模块里面的浅拷贝函数copy():
import copy will = ["Will", 28, ["Python", "C#", "JavaScript"]] wilber = copy.copy(will) print id(will) print will print [id(ele) for ele in will] print id(wilber) print wilber print [id(ele) for ele in wilber] will[0] = "Wilber" will[2].append("CSS") print id(will) print will print [id(ele) for ele in will] print id(wilber) print wilber print [id(ele) for ele in wilber]
使用copy模块里面的深拷贝函数deepcopy():
import copy will = ["Will", 28, ["Python", "C#", "JavaScript"]] wilber = copy.deepcopy(will) print id(will) print will print [id(ele) for ele in will] print id(wilber) print wilber print [id(ele) for ele in wilber] will[0] = "Wilber" will[2].append("CSS") print id(will) print will print [id(ele) for ele in will] print id(wilber) print wilber print [id(ele) for ele in wilber]
模块循环依赖
在Python中使用import导入模块的时候,有的时候会产生模块循环依赖,例如下面的例子,module_x模块和module_y模块相互依赖,运行module_y.py的时候就会产生错误。
# module_x.py import module_y def inc_count(): module_y.count += 1 print module_y.count # module_y.py import module_x count = 10 def run(): module_x.inc_count() run()
其实,在编码的过程中就应当避免循环依赖的情况,或者代码重构的过程中消除循环依赖。
当然,上面的问题也是可以解决的,常用的解决办法就是把引用关系搞清楚,让某个模块在真正需要的时候再导入(一般放到函数里面)。
对于上面的例子,就可以把module_x.py修改为如下形式,在函数内部导入module_y:
# module_x.py def inc_count(): import module_y module_y.count += 1
The above is the detailed content of Detailed explanation of Python traps and precautions commonly encountered in development. For more information, please follow other related articles on the PHP Chinese website!

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.

Python's real-world applications include data analytics, web development, artificial intelligence and automation. 1) In data analysis, Python uses Pandas and Matplotlib to process and visualize data. 2) In web development, Django and Flask frameworks simplify the creation of web applications. 3) In the field of artificial intelligence, TensorFlow and PyTorch are used to build and train models. 4) In terms of automation, Python scripts can be used for tasks such as copying files.

Python is widely used in data science, web development and automation scripting fields. 1) In data science, Python simplifies data processing and analysis through libraries such as NumPy and Pandas. 2) In web development, the Django and Flask frameworks enable developers to quickly build applications. 3) In automated scripts, Python's simplicity and standard library make it ideal.

Python's flexibility is reflected in multi-paradigm support and dynamic type systems, while ease of use comes from a simple syntax and rich standard library. 1. Flexibility: Supports object-oriented, functional and procedural programming, and dynamic type systems improve development efficiency. 2. Ease of use: The grammar is close to natural language, the standard library covers a wide range of functions, and simplifies the development process.

Python is highly favored for its simplicity and power, suitable for all needs from beginners to advanced developers. Its versatility is reflected in: 1) Easy to learn and use, simple syntax; 2) Rich libraries and frameworks, such as NumPy, Pandas, etc.; 3) Cross-platform support, which can be run on a variety of operating systems; 4) Suitable for scripting and automation tasks to improve work efficiency.

Yes, learn Python in two hours a day. 1. Develop a reasonable study plan, 2. Select the right learning resources, 3. Consolidate the knowledge learned through practice. These steps can help you master Python in a short time.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use