检测存在哪些可选参数的Pythonic方法
对python还是相当陌生的 我想知道什么是检测python程序选择的输出响应的好方法 例如,如果您要制作一个速度/距离/时间计算器,如果只给出2个输入,您将如何检测丢失的输入和输出?我可以想出一些相当粗糙的方法,但我想知道如果要执行更复杂的任务,是否还有其他方法 我猜是这样的:检测存在哪些可选参数的Pythonic方法,python,python-3.x,Python,Python 3.x,对python还是相当陌生的 我想知道什么是检测python程序选择的输出响应的好方法 例如,如果您要制作一个速度/距离/时间计算器,如果只给出2个输入,您将如何检测丢失的输入和输出?我可以想出一些相当粗糙的方法,但我想知道如果要执行更复杂的任务,是否还有其他方法 我猜是这样的: def sdf(speed=0, distance=0, time=0): # detect which parameter has no input / equals 0 # calculate resu
def sdf(speed=0, distance=0, time=0):
# detect which parameter has no input / equals 0
# calculate result
# return result
sdf(speed=10, distance=2)
有什么想法吗?您应该使用多个函数并调用所需的函数
def CalculateTravelTime(distance, speed)
def CalculateTravelSpeed(distance, time)
def CalculateTravelDistance(speed, time)
Python允许您动态更改变量的类型。由于您使用的是整数,
0
可能是计算中有用的值,因此默认的“不存在”值应为None
:
def sdf(speed=None, time=None, distance=None):
if speed is None:
return calculate_speed(time, distance), time, distance
if time is None:
return speed, calculate_time(speed, distance), distance
if distance is None:
return speed, time, calculate_distance(speed, time)
# All paramters have been set! Maybe check if all three are correct
return speed, time, distance
speed, time, distance = sdf(speed=1, distance=2)
这样你就不必知道后来发生了什么。此函数将为您提供所有三个值,前提是您至少提供了三个值中的两个
如果程序流允许多个值为None
,则函数calculate_XY
在检测到异常时应引发异常。因此,在这种情况下:
def calculate_distance(speed, time)
return speed * time
它将抛出一个不受支持的操作数异常(TypeError),因此不需要用无用的断言来混乱代码
如果您确实不知道将设置多少个参数,请执行以下操作:
try:
retval = sdf(None, None, x)
except TypeError as e:
print(e)
handle_exception(e)
还有一个提示:Python中的is
操作符检查对象是否是同一个对象,而不是它们的值。由于分配给None
的对象只是“指向全局None
对象”(简化)的指针,因此最好使用检查值“是否包含”None
。但是,请注意:
a = b = list()
a is b
True
# a and b are 'pointers' to the same list object
a = list()
b = list()
a is b
False
a == b
True
# a and b contain 2 different list objects, but their contents are identical
请注意,要比较值,请使用=
,要检查它们是否为同一对象,请使用is
HTH这就是我要做的:
def sdf(distance=None, speed=None, time=None):
"""Calculate the missing speed, distance time value
returns a 3-tuple (speed, distance, time)
raises ValueError if more than one or no unknowns are given"""
if (distance, speed,time).count(None) > 1:
raise ValueError('Error - more than one unknown provided')
if (distance, speed,time).count(None) == 0:
raise ValueError('Not sure what to calculate - all paramaters provided')
if speed is None:
return distance/time, distance, time
if time is None:
return speed, distance, distance/speed
if distance is None:
return speed, speed*time, time
请描述一下你想做什么。在没有上下文的情况下,使用常规条件检查参数是否具有默认值似乎是最好的选择。在您提供的情况下,sdf
函数的所有参数都具有默认值,并在调用时设置为0。当您以speed=10
和distance=2
呼叫时,time
将默认为0。但是我不能满意地回答这个问题,因为你所问的有点不清楚。@jaaq基本上我希望这个函数能做的就是计算已经提供的参数。如果函数接收到速度和距离,则计算时间并返回它,如果提供了时间和速度,则计算距离并返回该值。请确定为什么这是被否决的-即使最初的函数也做了所需的操作(尽管有点笨拙)。您的答案与Python Zen相反:检查多个None
类型是多余的,因为使用None
的算术无论如何都会导致抛出错误。计算0None
是多余的,因为程序将在所有3个条件返回False
后到达那里。并且您的元组创建有一个输入错误,如果stackoverflow允许编辑小到2个字符,我会编辑它<代码>距离。速度
应该是距离,速度jaaq-I explicity希望控制异常中的字符串,因此无计数并自定义异常,而不是仅仅由于计算中的无而引发无信息类型错误。我本可以用try/except来包装计算,但我选择了显式检查-我不明白为什么您认为python的“Zen”被违反了。@jaaq-感谢您对元组中句点(而不是逗号)的了解。谢谢,你没有回答这个问题。问题是,如果你只知道会得到3个参数中的2个,那么如何准确地知道要调用哪些函数。是的,但是当你调用sdf函数时,你知道你有哪些参数。因此,在那个时候,您可以根据需要选择不同的功能。一个函数做很多事情是不好的,如果输出是一致的,就不好了。无论如何,您必须检测程序中某一点上存在哪些参数。与不必要的分离相比,最好保持一致性和逻辑分组,imhoNo,您不必检测哪些参数是哪些参数。因为你知道,如果你不知道,你就不知道你给你的“sdf”函数指定了哪些参数,那么你的程序就永远不会工作。这是基于机会吗?我的答案中的函数是逻辑分组和一致的。然而,名为“sdf”的函数没有任何逻辑性,也不简洁。当我调用函数名“sdf”时,它并没有告诉我什么。我不知道它返回什么,我只知道我可以给它三个可选参数?我不赞成