8

我一直在使用的一些开源将以下行作为函数声明:

def parse_query(query=nil, options={}, models=nil)

“等号”符号对语句有什么影响?它只是使参数可选吗?

4

2 回答 2

13

如果调用函数的人没有指定参数,它会设置参数的默认值。

于 2009-06-10T15:39:34.887 回答
5

与 Python 和 C++ 类似,参数列表中的等号允许您指定默认参数。例如,在 Python 中:

def hello_world(message="Hello World"):
    print "message = "+message

像这样调用这个函数:

hello_world()

将导致:

message = Hello World

但是像这样调用函数:

hello_world("changed default")

结果是:

message = changed default
于 2009-06-10T15:43:22.070 回答