Syntactic sugar for assignment statements: one value to multiple targets?

Ethan Furman ethan at stoneleaf.us
Wed Aug 17 21:52:29 CEST 2011


gc wrote:
> Target lists using comma separation are great, but they don't work
> very well for this task. What I want is something like
> 
> a,b,c,d,e = *dict()

This isn't going to happen.  From all the discussion so far I think your 
best solution is a simple helper function (not tested):

def repeat(count_, object_, *args, **kwargs):
     result = []
     for _ in range(count_):
         result.append(object_(*args, **kwargs))
     return result

a, b, c, d, e = repeat(5, dict)

These are each new objects, so depending on the function (like the 
random.rand_int example) the values may not be the same.

Oh, and I put the trailing _ on count and object to minimize possible 
conflicts with keyword arguments.

~Ethan~



More information about the Python-list mailing list