Concise idiom to initialize dictionaries

Peter Otten __peter__ at web.de
Wed Nov 10 04:58:30 EST 2004


Rainer Mansfeld wrote:

> Being a C/C++ programmer with only a couple of weeks of Python
> experience, I'd simply do:
> 
> a = b = c = d = e = f = g = h = i = j = k = l = {}
> 
> That's clear and compact, but is it 'pythonic'?

It is usually wrong as all names "point" to the _same_ dictionary:

>>> a = b = c = d = e = f = g = h = i = j = k = l = {}
>>> a[1] = 42
>>> b
{1: 42}
>>> l
{1: 42}

The OP wanted all variables initialized with a _separate_ dictionary.
If you really want the above behaviour, from my point of view there is
nothing speaking against chaining assignments.

Peter





More information about the Python-list mailing list