The most efficient way to assign multiple variables to zero at the same time

I am trying to initialize the variables to zero, so currently it looks like

x1,y1,x2,y2=(0,0,0,0) 

It works, but just seems a little redundant. Is there a cleaner way?

+8
python
source share
3 answers

I usually did

 x1 = y1 = x2 = y2 = 0 

However, this is hardly relevant. Both versions are easy to understand at a glance.

+10
source share

This effectively unpacks the tuple. You can do:

 x1 = y1 = x2 = y2 = 0 

Just don't do this with mutable objects!

+8
source share

I personally used Jon or Sven for descriptive itertools , but as an alternative answer you can use itertools for this:

 import itertools x1,y1,x2,y2 = itertools.repeat(0,4) 

Warning about mutable objects is still applied!

0
source share

All Articles