The default parameter of python functions
2013-04-03 17:52
260 查看
As I've said in a previous article, every thing in python is a object, maybe there are exceptions, but it's good enough to think in this way at the beginning.
So, function is a object.
Then, this deviates some thing we may take care of.
First, we should know, the definition of a function is in fact a executable statement.
e.g. def hello(names = []):
names.append('tom')
for i in names:
print 'hello'
the line def hello(): is executed. Creating an function object which can be used in the current region, but the body is not executed.
Then each time the function is called, the body is executed with this object.
So what to be careful of? The default parameter.
Because the object is only created once, the default parameter is generated in that, and if it's mutable, problems come.
As in the code above, you may see that more and more 'tom' gets into the list each time.
Then how to avoid it?
use None to initialize it or otherwise, you take care of it yourself.
So, function is a object.
Then, this deviates some thing we may take care of.
First, we should know, the definition of a function is in fact a executable statement.
e.g. def hello(names = []):
names.append('tom')
for i in names:
print 'hello'
the line def hello(): is executed. Creating an function object which can be used in the current region, but the body is not executed.
Then each time the function is called, the body is executed with this object.
So what to be careful of? The default parameter.
Because the object is only created once, the default parameter is generated in that, and if it's mutable, problems come.
As in the code above, you may see that more and more 'tom' gets into the list each time.
Then how to avoid it?
use None to initialize it or otherwise, you take care of it yourself.
相关文章推荐
- [Yii Framework] Set the default parameter of widgets for the whole application
- The Story of self Parameter in Python, Demystified
- The study of parameter of function in Python(20170908)
- The use of functions
- 'You are trying to run the Python 2 version of Beautiful Soup under Python 3. This will not work.'<>
- Error 47 "xxxxxxx"is an invalid value for the "InputManifest" parameter of the "GenerateApplicationM
- hdu 5095 Linearization of the kernel functions in SVM(水题)
- python the method of super function
- 错误:“The content of element type "package" must match "(result-types?,interceptors?,default-intercept
- windows下python 安装igraph库报错 Cannot find the C core of igraph on this system using pkg-config.的解决
- The content of element type "package" must match "(result-types?,interceptors?,default-intercept .
- spring boot 报错:Your ApplicationContext is unlikely to start due to a @ComponentScan of the default p
- How to change the default run level of a RedHat 9.0 or Fedora Core Linux system
- default argument given of parameter 的问题
- No orientation specified, and the default is horizontal. This is a common source of bugs when childr
- struts2---The content of element type "package" must match "(result-types?,interceptors?,default-int
- Spring MVC How take the parameter value of a GET HTTP Request in my controller method?
- The Steps of Machine Learning with Python
- centos上执行SVN的mailer.py时报错You need version 1.5.0 or better of the Subversion Python bindings.