The easiest way to initialize a large number of variables

Suppose you are given a large number of variables that need to be initialized to None. A naive way to do it is to count the number of variables on the left and create a list with the same size on the right:

a, b, c, d, e, f, g, h, i, j = [None]*10

      

Apart from variables, is there a way to do this? If you use this pattern a lot, you might need to count the number of variables.

+3


source to share


2 answers


a = b = c = d = e = f = g = h = i = j = None

      

Note. Don't use this for mutable types. If you're interested, this demonstrates:



>>> a = b = []
>>> a.append(1)
>>> a
[1]
>>> b
[1]

      

+12


source


Is there a reason why you don't?

a = b = c = d = e = f = g = h = i = j = None

      



I am not very knowledgeable about the intricacies of Python syntax, so please correct me if I am wrong.

+5


source







All Articles