defaultdict(lambda: defaultdict(dict)) 的含义
Meaning of defaultdict(lambda: defaultdict(dict))
Python中的下一行是什么意思?
x = defaultdict(lambda: defaultdict(dict))
让我们从内到外解决它。首先,dict
is the dictionary type. Like other types, calling it creates an instance (also known as object) of that type. A defaultdict
is a type that takes a callable parameter: something that, when called, produces an item to put in the dictionary. This happens when an entry is accessed that was not present, instead of producing a KeyError
like an ordinary dict
. Thirdly, lambda
是一种基于单个表达式创建未命名函数的方法,所以这两个是相似的(第二个拥有一个知道自己名字的函数,第一个不知道):
y = lambda: defaultdict(dict)
def y():
return defaultdict(dict)
最后整个东西都包裹在另一个 defaultdict
中。所以结果是 x
是一个 defaultdict
产生 defaultdict
产生 dict
个实例。在第三级不再有默认值。
Python中的下一行是什么意思?
x = defaultdict(lambda: defaultdict(dict))
让我们从内到外解决它。首先,dict
is the dictionary type. Like other types, calling it creates an instance (also known as object) of that type. A defaultdict
is a type that takes a callable parameter: something that, when called, produces an item to put in the dictionary. This happens when an entry is accessed that was not present, instead of producing a KeyError
like an ordinary dict
. Thirdly, lambda
是一种基于单个表达式创建未命名函数的方法,所以这两个是相似的(第二个拥有一个知道自己名字的函数,第一个不知道):
y = lambda: defaultdict(dict)
def y():
return defaultdict(dict)
最后整个东西都包裹在另一个 defaultdict
中。所以结果是 x
是一个 defaultdict
产生 defaultdict
产生 dict
个实例。在第三级不再有默认值。