python 经典的参数错误-Python 技术分享 Java技术分享 Python 爬虫技术_微信公众号:zeropython—昊天博客

 

Important warning: The default value is evaluated only once. This makes a difference when the default is a mutable object such as a list, dictionary, or instances of most classes. For example, the following function accumulates the arguments passed to it on subsequent calls:

重要警告:默认值只计算一次。当默认值是可变对象(如列表,字典或大多数类的实例)时,这会有所不同。例如,以下函数会累积在后续调用中传递给它的参数

 

例子一:

 

 

输出值为5,不是6

例子二:

输出值为

[1]

[1, 2]

[1, 2, 3]

这个原因是由于默认参数只计算一次,因为list 是可变数据类型,函数每次调用时,L 是同一个对象的引用。

加一个id(),可以判断每次函数的调用都是访问的同一个list 实例对象。

python 经典的参数错误-Python 技术分享 Java技术分享 Python 爬虫技术_微信公众号:zeropython—昊天博客

HTTPX 基础教程-新乡seo|网站优化,网站建设_微信公众号:zeropython—昊天博客