


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!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.
