I'm new to Python. I heard that when we initialize a function we should always st开发者_如何学JAVAart by self but I didn't get the fileName=None
. What does this argument stand for?
def __init__(self, fileName=None):
It is a default value for the parameter fileName
. If the caller does not specify a value, it will be set to None
. This code demonstrates it:
def foo(bar=None):
print bar
foo() # will print the default value
>> None
foo(1) # will print the given value
>> 1
Note that default parameter values in Python are tricky, because the expression will be evaluated exactly once, when the function definition is executed.
精彩评论