如何在函数中声明一个可选参数,在Python中至少有一个实数(非可选)参数?你知道吗
举个例子:
def myfunc(data, mode='never_mind'):
if mode == 'never_mind:
return
elif mode == 'print':
print "Input data:", data
# do something with data...
elif mode == 'sqrt':
print "%f is a square root of %f (input data)" % (data ** 0.5, data)
else:
print "Invalid input mode."
# I want to declare a new function as myfunc() with predeclared mode='sqrt' (for example).
# How can I do it?
new_func = myfunc
new_func.mode = 'sqrt' # it doesn't work!
# or...
new_func = myfunc(mode='sqrt') # it doesn't work either!
签出
functools.partial
。你知道吗相关问题 更多 >
编程相关推荐