Write the If Statement of Python to avoid placing the branch code in the same line after the exit
Using indentation to differentiate scopes (the same as other places in Python) makes it easier to determine what conditions are to be executed.If,Elif, AndElseThe statement must always be in the new line. The code should not be followed:.
Bad
name = 'Aidan'address = 'GuangDong, ShenZhen'if name: print(name)print(address
Recommended
name = 'Aidan'address = 'GuangDong, ShenZhen'if name: print(name)print(address
Avoid repeated variables in If language
When you want to check whether a variable is a series of values, it is unnecessary to check the value repeatedly. UseIterable(Iteration) makes the code simpler and readable
Bad
is_generic_name = Falsename = 'Tom'if name == 'Tom' or name == 'Dick' or name == 'Harry': is_generic_name = True
Recommended
name = 'Tom'is_generic_name = name in ('Tom', 'Dick', 'Harry')
Avoid direct comparison between True, False, or None.
For any object, both built-in and user-defined objects have related Boolean features. When checking whether a condition is true, if it is not in a statement that strictly judges its object Boolean features, judging rules is a bit strange. The following will be consideredFalse:
- None
- False
- Value 0
- Empty Sequence
- Empty dictionary
- Object Method_ LenOr_ NonzoreThe return value is 0 or False.
All of these items are consideredTrue(Or implicit is True ). The final condition is to check the object'sLenOrNonzeroTo determine the Boolean value.
IfThe statement uses a Boolean value. You should not use the following values:
if foo == True:
Simple use of if foo :.
There are several reasons. The most important thing is whenFooChangeIntType,IfThe statement is still valid. The deeper reason is thatEqualityAndIndentity. Use = to determine whether two objects have the same value (_ eq attribute ). Use is to determine whether two objects are the same.
Avoid directly followingFalseAndNoneOr empty sequence (for example, [], {}) for comparison. If my_lists is an empty list, if mylist: False is returned.
Sometimes, although direct comparison with None is not recommended, it is sometimes necessary. The function checks whether the default value of a parameter is None. For example:
def insert_value(value, position=None): """INserts a value into my container, optionally at the specified position""" if position is not None: ...
Here if position :? What have you done? When someone wants to insert a value to the specified 0 position, when the function does not set the position parameter, since 0 is equal to False, we use is or is not for None comparison, instead of =.
Bad
def number_of_evil_robots_attacking(): reutrn 10def should_raise_shields(): # "We only raise Shields when one or more giant robots attack, # so I can just return that value..." return number_of_evil_robots_attacking()if should_raise_shields() == True: raise_shields() print("Shields raised") else: print("Safe! No giant orbots attacking")
Recommended
def number_of_evil_robots_attacking(): reutrn 10def should_raise_shields(): # "We only raise Shields when one or more giant robots attack, # so I can just return that value..." return number_of_evil_robots_attacking()if should_raise_shields(): raise_shields() print("Shields raised") else: print("Safe! No giant orbots attacking")