在Python中,可以通过以下方式找出两个参数中哪个是在函数调用时传递的:
def compare_parameters(param1, param2):
if param1 is param2:
print("param1 and param2 are the same")
elif param1 == param2:
print("param1 and param2 have the same value but are different objects")
else:
print("param1 and param2 are different")
compare_parameters(10, 10) # Output: param1 and param2 have the same value but are different objects
def compare_parameters(param1=None, param2=None):
if param1 is param2:
print("param1 and param2 are the same")
elif param1 == param2:
print("param1 and param2 have the same value but are different objects")
else:
print("param1 and param2 are different")
compare_parameters(param1=10, param2=10) # Output: param1 and param2 have the same value but are different objects
通过使用位置参数或关键字参数,可以明确指定参数的传递方式,从而确定哪个参数是在函数调用时传递的。
领取专属 10元无门槛券
手把手带您无忧上云