Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在返回的元组中为";使用布尔值;如果;陈述_Python_Python 2.7 - Fatal编程技术网

Python 在返回的元组中为";使用布尔值;如果;陈述

Python 在返回的元组中为";使用布尔值;如果;陈述,python,python-2.7,Python,Python 2.7,当前情况: def isTooLarge(intValue): if intValue > 100: print "too large"; return True return False if isTooLarge(101): break 现在,我想通过返回errormessagetext而不是打印它,使函数更“库友好”: def isTooLarge(intValue): if intValue > 100: return True, "too large"

当前情况:

def isTooLarge(intValue):
  if intValue > 100: print "too large"; return True
  return False

if isTooLarge(101): break
现在,我想通过返回errormessagetext而不是打印它,使函数更“库友好”:

def isTooLarge(intValue):
  if intValue > 100: return True, "too large"
  return False

bln,str = isTooLarge(101)
if bln: specialprint(str); break
你知道我怎样才能把它重新评价为一行吗?(类似于“if,str-isTooLarge(101):specialprint(str);break)”或者Python的方式是什么

将errormessage放入“lasterrormessage”这样的全局变量并保持其余变量不变没有问题

def isTooLarge(intValue):
    return "too large" if intValue > 100 else False

x = isTooLarge(101); x and print(x)
但是,请不要这样做。异常的存在是有原因的。仅仅为了它而将内容放在一行中会使您的代码难以阅读。

通过使用错误,您可以更加“库友好”,就像使用错误一样,例如错误:

def CheckNotTooLarge(intValue):
    if intValue > 100:
        raise ValueError("too large") #or AssertionError
    return #or maybe do something else?
然后用户可以使用
try:except:

try:
   CheckNotTooLarge(101)
except ValueError:
    traceback.print_exc() #print error message
    #handle too large
else:
    #handle not too large
我可以看出,如果您只想在不处理错误的情况下进行检查,这会很快变得很烦人,因此我建议使用两个函数,一个函数只返回布尔值,不需要额外的工作,另一个函数引发/返回错误文本:

def isTooLarge(intValue):
    return intValue<=100 #now this is a one liner!

def checkIsTooLarge(intValue):
    "uses isTooLarge to return an error text if the number is too large"
    if isTooLarge(intValue):
       return "too large" #or raise ...
    else:
       return False
def isTooLarge(intValue):

返回intValueum…
如果iStorage(101):打印(“太大”);break
让调用ISTOOLAGE的任何函数处理错误消息/文本。据我所知,如果不对ISTOOLAGE进行两次评估,就不会有任何类似的东西。但您编写的可能是最清晰的方法。否则,有两个函数,一个只返回True或False,另一个只返回True或False调用第一个函数,但如果它返回True,则具有额外逻辑的函数。坦率地说,我不确定您使用这个错误文本的目的是什么。请尝试
isTooLarge(101)[0]
.Caveat,我不是Python高手,所以如果它不起作用,你会被警告。看,听起来你几乎应该抛出一个异常。例如,
raisevalueerror(“太大”)
。然后,callie必须处理它。我同意ValueError更适合这种检查,尽管当函数名为
assert\uuuuuuuuu
时,我可能希望它会引发一个
AssertionError
。在本例中,我将该函数的名称更改为
dosomething
并表示将
return False
#do some here
放在一起。在我看来,断言是一个不同的主题。无论如何,这只是一个小问题。但是@rkersh问题中的函数没有做任何其他事情……无论如何,ValueError绝对是正确的异常类型,并在可能的情况下添加注释。谢谢我的建议有很多。我只是忘记了Python中存在“异常”。另一方面,我通常将它们用于实际的异常错误(例如意外的EOF、套接字关闭),并尝试在flowcontrol中避免它们,因为它们对CPU来说是昂贵的,并且很难读取(至少在Java中)。