python - Why do you need lambda to nest defaultdict? -


i bit confused on why need lambda function nesting defaultdict

why can't this?

test = defaultdict(defaultdict(list)) 

instead of

test = defaultdict(lambda:defaultdict(float)) 

test = defaultdict(defaultdict(list)) 

because defaultdict requires give can called create keys missing values. list such callable, defaultdict(list) not. it's defaultdict instance, , can't call defaultdict.

the lambda function that, when called, returns value can used in dictionary, works.

essentially, defaultdict(list) going evaluated before defaultdict instantiated, , want defer until missing key encountered. why callable object (a type or function) used here.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -