Python 3.x Python else vs unindent
假设有一个带有条件的Python 3.x Python else vs unindent,python-3.x,Python 3.x,假设有一个带有条件的if和else的代码。我注意到,通常情况下,您不必在代码末尾编写else,而是可以取消对它的插入,这会使它稍微优雅一些,并至少保存一行。例如: def letter_check(word, letter): if word in letter: return True else: return False vs 这两个代码将输出相同的结果。你能给我举个例子吗,在那里,或者会有所不同或者是有利的?我说的不是elif。 谢谢对于return语句没有关系,
if
和else
的代码。我注意到,通常情况下,您不必在代码末尾编写else
,而是可以取消对它的插入,这会使它稍微优雅一些,并至少保存一行。例如:
def letter_check(word, letter):
if word in letter:
return True
else:
return False
vs
这两个代码将输出相同的结果。你能给我举个例子吗,在那里,或者
会有所不同或者是有利的?我说的不是elif。
谢谢对于
return
语句没有关系,因为return
退出函数,但是如果您想做其他事情,它可以不同:
def letter_check(word, letter):
if word in letter:
print "1"
else:
print "2"
--> 1 # if true
--> 2 # if false
def letter_check(word, letter):
if word in letter:
print "1"
print "2"
--> 1 --> 2 # if true
--> 2 # if false
通常的做法是在函数体的末尾返回函数的结果。在这种情况下,将使用
result
变量,并在函数末尾返回该变量
编辑:我将更改回答中问题的原始示例,因为True/False
模式无助于理解
def compute_word_len(word, with_digits=False):
""" Return the size of the given word with or without
the digits according to the with_digits parameter.
"""
if with_digits:
result = len(word)
else:
result = len(''.join([c for c in word if not c.isdigit()]))
return result
如果If/else
块是二进制的,则可以用默认值初始化变量,并在If
块中进行更改。只有当计算默认值的函数不耗时时,这种情况才有用
def compute_word_len(word, with_digits=False):
""" Return the size of the given word with or without
the digits according to the with_digits parameter.
"""
result = len(word)
if not with_digits:
result = len(''.join([c for c in word if not c.isdigit()]))
return result
另一种做法是避免函数不应该运行的情况。这可以避免将函数体写入不必要的缩进if
块:
def compute_word_len(word, with_digits=False):
""" Return the size of the given word with or without
the digits according to the with_digits parameter.
"""
if not word:
return 0
result = len(word)
if not with_digits:
result = len(''.join([c for c in word if not c.isdigit()]))
return result
两个功能的操作代码略有不同:
def无其他(x):
如果x==0:
返回0
返回1
def和_else(x):
如果x==0:
返回0
其他:
返回1
进口dis
打印(“---不带其他--”)
dis.dis(无其他)
打印(“---加上其他--”)
dis.dis(与其他人一起)
没有其他选项:
与else:
请注意,从指令0-14中,两个函数给出了完全相同的操作码。唯一的区别是在with_else
函数的末尾添加了一个隐式return None
。这不太可能以任何方式影响性能(尽管编译隐式返回None
所需的开销很小)
就我个人而言,我更喜欢在可能的情况下使用
而不使用其他;我觉得它看起来更好。我发现第一个片段更明确。我想这就是为什么我发现它比第二个片段更优雅的原因。话虽如此,我认为在这种情况下,函数没有什么不同。在if
分支中,用任何不返回/抛出/否则离开函数的东西替换return…
。请注意,整个函数体应该是返回字母中的单词。扔掉一个微不足道的else
是一条红鲱鱼。是的,我犹豫着说给出的例子相对于这个问题不是很准确。我将编辑我的答案。请注意,通过sum(如果不是c.isdigit(),则word中的c为1)计算非数字长度将更有效。
。当前版本不需要在内存中创建完整的列表和字符串。您所说的-->1-->2是什么意思?据我所知,第二个函数总是输出“2”。但是,检查代码后,如果条件为真,则输出将为“1”。这意味着它将输出1,然后输出2,即如果字母中的单词
def compute_word_len(word, with_digits=False):
""" Return the size of the given word with or without
the digits according to the with_digits parameter.
"""
if not word:
return 0
result = len(word)
if not with_digits:
result = len(''.join([c for c in word if not c.isdigit()]))
return result
2 0 LOAD_FAST 0 (x)
2 LOAD_CONST 1 (0)
4 COMPARE_OP 2 (==)
6 POP_JUMP_IF_FALSE 12
3 8 LOAD_CONST 1 (0)
10 RETURN_VALUE
4 >> 12 LOAD_CONST 2 (1)
14 RETURN_VALUE
2 0 LOAD_FAST 0 (x)
2 LOAD_CONST 1 (0)
4 COMPARE_OP 2 (==)
6 POP_JUMP_IF_FALSE 12
3 8 LOAD_CONST 1 (0)
10 RETURN_VALUE
5 >> 12 LOAD_CONST 2 (1)
14 RETURN_VALUE
16 LOAD_CONST 0 (None) # This is the implicit return
18 RETURN_VALUE # None added by python