In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what are the common mistakes in writing Python code". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what are the common mistakes in writing Python code.
Write code that is too stylized
This is a typical feature of Python beginners. In order to write code similar to advanced pseudo English, they eventually added the following types of code snippets to their code base:
If x = = 1 or x = = 2
It looks good. This line of code means that the variable x must be 1 or 2 to meet the condition. However, such code snippets are too stylized and affect readability. The following alternative code snippet is easy to understand, which checks to see if the value belongs to an element in the list:
If x in [1,2]
Unnecessary comparison operators: None and zero
Programmers with a Java background know how many null checks are required (especially in versions prior to Java 8). Therefore, it is not surprising to see such a comparison operator in Python:
A = = None b! = None
The above situation can use python to write code to enhance readability:
An is None b is not None
It is also worth noting that for 0, there is actually no need to use the comparison operator in the conditional logic. 0 is interpreted as false, while non-zero numbers are treated as true.
Use long chain conditional bit logic
In most languages, including Swift,Java,Kotlin, some comparison logic can be written in the following ways:
If a
< b < c 大多数语言不能在非关联优先级中使用相邻运算符,而Python则不同,Python可以链式赋值,如以下代码所示: if a < b < c 因此,这样做可以避免按位运算符。 使用type()代替isinstance(),反之亦然 type和isinstance是Python中用于类型检查的两个广泛使用的内置函数。通常,新手开发人员会认为这两个函数很相似并互换使用。这可能引发无法预料的错误,因为type()和isinstance()具有一些细微的差异。 isinstance()函数用于检查对象是否是指定类的实例,同时还要注意继承。另一方面,type()仅检查引用类型是否相等,并丢弃子类型。因此,以下代码使用type()和isinstance()给出了不同的结果: class Vehicle: pass class Car(Vehicle): passisinstance(Car(), Vehicle) #returns True type(Car()) == Vehicle # returns False 同样,以下代码将布尔值视为int的实例(因为True和False基本上被视为1和0),但是使用type函数给出了不同的结果。 type(True) == int # falseisinstance(True, int) # trueisinstance(False,int) # true 因此,重要的是要了解Python的两个类型检查器函数之间的差异,并且不要彼此混淆。 混淆作用域中的局部变量和全局变量 Python中的作用域规则看起来相当简单,但很容易造成误解。例如,以下代码在函数内部使用全局变量: a = 10 def printMe(): print(a)printMe() # prints 10 如果通过修改函数中的变量来稍微调整上述代码,就会抛出错误: a = 20 def printA(): print(a) a = 10print(a) # gives 20 printA() # gives error as a is referenced before assigned 一旦在函数内部修改了全局变量,Python就会将其视为局部变量,从而覆盖全局变量。甚至赋值前的打印语句也没有执行。 为确保此类名称冲突不会导致错误,可以在局部函数内为全局变量附加global关键字。甚至最好将全局变量(如果确实需要使用)放在单独的类中,以便始终将全局变量与类名一起使用。 可变默认参数 在Python中,使用默认参数很常见,它可以避免在调用函数时出现一长串参数。列表、字典和集合是Python中的可变类型。设置默认值会导致意外结果,如下所示: def addToList(x, a=[]): a.append(x) return alistOne = addToList(5) #prints [5]anotherList = addToList(10) # [5, 10] 如你所见,第二个列表包含先前添加的元素,因为函数中的可变默认参数将它们存储在各个状态之间。 Python中可变默认对象的问题表现在定义函数时会对其进行评估,这会导致可变值也保存先前的内容。为避免此类严重的错误,请将None设置为默认值,然后在函数内分配可变变量,如下所示: def addElement(x, a=None): if not a: a = [] a.append(x) return a 忽略多重继承和方法解析顺序 图源:unsplash 与大多数语言不同,Python支持多重继承。即在具有继承的类中,方法和类变量将根据继承类时指定的顺序执行。初学者通常会忽略此概念,尤其是在仅使用单一继承的情况下。在下面的代码中,当调用C类的方法时,将使用超类B的相应方法: >> > class A (object): Def me (self): print ("class A") > > class B (A): Def me (self): print ("class B") class C (B, A): passc = C () c.me () # prints class B
The order of inherited classes in Python is important, and it can be used to solve these problems.
Python is simple, but be careful not to be confused with other languages, which can lead to strange errors and program crashes. I hope the above summary will help you sort out the concept and write more stable Python code.
Thank you for reading, the above is "what are the common mistakes in writing Python code?" after the study of this article, I believe you have a deeper understanding of the common mistakes in writing Python code, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.