例如:
#creating the function with params
def symlinks(release_path, deploy_to, settings_file, roll_back=False, is_file=False):
#doing something with all the params
#executing the function
symlinks(releasepath, deploypath, settingsfile, is_file=true)
所以在python中有多种方法将参数传递给函数,上面的一个有效吗?
我尝试了symlinks(releasepath,deploypath,True)但似乎影响了roll_back。 roll_back参数是否仍然是假的?
答案 0 :(得分:1)
可以说,找到问题答案的最酷方法是使用inspect.getcallargs
。你可以通过玩它来学到很多东西。
import inspect
inspect.getcallargs(symlinks, 'releasepath', 'deploypath', 'settingsfile', is_file=True)
=>
{'deploy_to': 'deploypath',
'is_file': True,
'release_path': 'releasepath',
'roll_back': False,
'settings_file': 'settingsfile'}
答案 1 :(得分:1)
Python会按照传递的顺序将传入的值绑定到位置参数名称,除非您明确指定参数名称。
在您的情况下,如果您使用symlinks(releasepath,deploypath,True)
调用该函数,则默认值将用于剩余的2个参数(roll_back
和is_file
)。
请参阅文档:https://docs.python.org/2/tutorial/controlflow.html#keyword-arguments