- 主题:这个Python代码怎么理解?
In [4]: def g(x=[]):
...: pass
...:
In [5]: g.__defaults__
Out[5]: ([],)
【 在 ooolinux 的大作中提到: 】
: 这个属性是啥属性,怎么查看?
--
FROM 221.216.209.62
人家文档里写了,为啥叫随意性
【 在 kawolu 的大作中提到: 】
: 这就是py的随意性嘛,很恶心的,而用这种随意性出题,就更恶心了。
--
FROM 221.216.209.62
class A():
def __init__(self):
print('in A')
def f(x=A()):
pass
if __name__ == '__main__':
print('main')
这段代码,虽然在执行时没有调用f(),但是会输出一次'in A',就是为了生成一个默认值x
【 在 ooolinux 的大作中提到: 】
: 不是很明白
--
FROM 221.216.209.62
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:
在官方文档的The Python Tutorial里就有
【 在 nokia0125 的大作中提到: 】
: 请问,哪里文档写了,
--
修改:annals FROM 221.216.209.62
FROM 221.216.209.62