我有一个方法,按顺序调用其他4个方法来检查特定的条件,并在每次返回Truthy时立即返回(不检查以下的方法).
def check_all_conditions():
x = check_size()
if x:
return x
x = check_color()
if x:
return x
x = check_tone()
if x:
return x
x = check_flavor()
if x:
return x
return None
Run Code Online (Sandbox Code Playgroud)
这似乎是很多行李代码.而不是每个2行if语句,我宁愿做类似的事情:
x and return x
Run Code Online (Sandbox Code Playgroud)
但那是无效的Python.我在这里错过了一个简单优雅的解决方案吗?顺便说一下,在这种情况下,这四种检查方法可能很昂贵,所以我不想多次调用它们.
我正在尝试学习python,并且遇到了一些既好又短的代码,但并不完全有意义
背景是:
def fn(*args):
return len(args) and max(args)-min(args)
Run Code Online (Sandbox Code Playgroud)
我得到它正在做的事情,但为什么python这样做 - 即返回值而不是True/False?
10 and 7-2
Run Code Online (Sandbox Code Playgroud)
返回5.同样,更改和将导致或将导致功能更改.所以
10 or 7 - 2
Run Code Online (Sandbox Code Playgroud)
会回来10.
这是合法/可靠的风格,还是有任何问题?
例如,如果我有以下声明:
if( foo1 or foo2)
...
...
Run Code Online (Sandbox Code Playgroud)
如果foo1为true,python会检查foo2的条件吗?
为什么下面提到的程序的输出0不是20?
#include <stdio.h>
int main()
{
int i = 10, j = 0;
if (i || (j = i + 10))
/* do something */;
printf("%d\n",j);
}
Run Code Online (Sandbox Code Playgroud) 我正在经历LPTHW,我遇到了一些我无法理解的东西.什么时候你想要你的布尔值and或or返回布尔值以外的东西?LPTHW文本指出像python这样的所有语言都有这种行为.他的意思是解释与编译语言或鸭类型与静态类型语言?
我运行了以下代码:
>>> False and 1
False
>>> True and 1
1
>>> 1 and False
False
>>> 1 and True
True
>>> True and 121
121
>>> False or 1
1
>>> False or 112
112
>>> False or "Khadijah"
'Khadijah'
>>> True and 'Khadijah'
'Khadijah'
>>> False or 'b'
'b'
>>> b = (1, 2, "K")
>>> b
(1, 2, 'K')
>>> False or b
(1, 2, 'K')
>>>
Run Code Online (Sandbox Code Playgroud)
请帮我理解这里发生的事情.
在Python中我们可以这样做:
if True or blah:
print("it's ok") # will be executed
if blah or True: # will raise a NameError
print("it's not ok")
class Blah:
pass
blah = Blah()
if blah or blah.notexist:
print("it's ok") # also will be executed
Run Code Online (Sandbox Code Playgroud)
我很好奇如果第一个条件返回 False,Python 是否会继续检查 if 语句中的条件。我想知道这一点,因为我想知道最佳实践是否是在进行更复杂的检查之前尝试检查时间复杂度较低的条件。
这两个片段有什么区别吗?
if condition_1() and condition_2():
do_something()
Run Code Online (Sandbox Code Playgroud)
和
if condition_1():
if condition_2():
do_something()
Run Code Online (Sandbox Code Playgroud) if cpu_expensive_condition() or simple_condition():
do_something()
Run Code Online (Sandbox Code Playgroud)
在上面的python代码的OR语句中有两个条件,它将首先进行评估?,是否必须对两者进行评估?
我已经制作了一个程序,但是如果用户输入不是二进制格式,我想添加一个例外.我已经多次尝试添加异常,但我似乎无法让它工作.以下是程序代码.如果有人可以帮忙,我将不胜感激.
import time
error=True
n=0
while n!=1:
print"***Welcome to the Bin2Dec Converter.***\n"
while error:
try:
bin2dec =raw_input("Please enter a binary number: ")
error=False
except NameError:
print"Enter a Binary number. Please try again.\n"
time.sleep(0.5)
except SyntaxError:
print"Enter a Binary number. Please try again.\n"
time.sleep(0.5)
#converts bin2dec
decnum = 0
for i in bin2dec:
decnum = decnum * 2 + int(i)
time.sleep(0.25)
print decnum, "<<This is your answer.\n" #prints output
Run Code Online (Sandbox Code Playgroud) 所以,我正在玩翻译,并输入以下内容:
In [95]: 1 and 2
Out[95]: 2
In [96]: 1 and 5
Out[96]: 5
In [97]: 234324 and 2
Out[97]: 2
In [98]: 234324 and 22343243242
Out[98]: 22343243242L
In [99]: 1 or 2 and 9
Out[99]: 1
Run Code Online (Sandbox Code Playgroud)
最初我认为它与False和True值有关,因为:
In [101]: True + True
Out[101]: 2
In [102]: True * 5
Out[102]: 5
Run Code Online (Sandbox Code Playgroud)
但这似乎并不相关,因为False总是为0,而且从上面的试验来看,它似乎不是输出的最大值.
我不能在这里诚实地看到这种模式,并且在文档中找不到任何东西(老实说,我真的不知道如何有效地寻找它).
那么,怎么做
int(x) [logical operation] int(y)
Run Code Online (Sandbox Code Playgroud)
在Python工作?
python ×9
boolean ×2
c ×1
if-statement ×1
logic ×1
operands ×1
operators ×1
performance ×1
python-2.7 ×1
python-3.x ×1