我一直在使用的一些开源将以下行作为函数声明:
def parse_query(query=nil, options={}, models=nil)
“等号”符号对语句有什么影响?它只是使参数可选吗?
我一直在使用的一些开源将以下行作为函数声明:
def parse_query(query=nil, options={}, models=nil)
“等号”符号对语句有什么影响?它只是使参数可选吗?
如果调用函数的人没有指定参数,它会设置参数的默认值。
与 Python 和 C++ 类似,参数列表中的等号允许您指定默认参数。例如,在 Python 中:
def hello_world(message="Hello World"):
print "message = "+message
像这样调用这个函数:
hello_world()
将导致:
message = Hello World
但是像这样调用函数:
hello_world("changed default")
结果是:
message = changed default