python 文件名=None
我刚开始学习Python。我听说在定义一个函数时,我们应该总是以self开头,但我不太明白fileName=None
这个参数是什么意思。
def __init__(self, fileName=None):
1 个回答
2
这是参数 fileName
的默认值。如果调用这个函数的人没有提供值,它就会被设置为 None
。下面的代码演示了这一点:
def foo(bar=None):
print bar
foo() # will print the default value
>> None
foo(1) # will print the given value
>> 1
需要注意的是,Python 中的默认参数值有点复杂,因为这个值的计算只会在函数定义的时候进行一次,具体可以参考这个链接。