12

I'm writing a function to calculate calendar dates. While cutting down on lines, I've found that I am unable to assign multiple variables to the same range.

Jan, Mar, May, Jul, Aug, Oct, Dec = range(1,32)

Would there be an efficient way to assign these values and why does python give a ValueError?

tijko
  • 7,599
  • 11
  • 44
  • 64
  • 3
    Is there any reason you're not using the datetime module? http://docs.python.org/library/datetime.html – robert Jun 01 '12 at 21:38

2 Answers2

68

Use

Jan = Mar = May = ... = range(1, 32)
Guido van Rossum
  • 16,690
  • 3
  • 46
  • 49
  • 46
    I was typing out an answer just as you submitted yours, and then realized there is no way to give a better answer than the inventor of python :-) – jdi Jun 01 '12 at 21:39
  • 1
    @tijko: It does allow it. But the result of `range(x)` is trying to be unpacked into the variables on the left. This works: `a,b = 1,2` – jdi Jun 01 '12 at 21:45
  • @jdi: Thanks for the reply, I meant why is that multiple variables can't be assigned to the same range? – tijko Jun 01 '12 at 21:53
  • 1
    @tijko: I'm not sure if many languages would do that using the comma-separated syntax. I would guess in python, its because then it would break value unpacking functionality. – jdi Jun 01 '12 at 21:57
  • @jdi: Right after I re-phrased the question I read CrazyJugglerDrummers answer and it laid out a nice explanation. Thanks. – tijko Jun 01 '12 at 22:03
  • @tijko `Jan, Mar, May = range(3)` means "unpack the three items in `range(3)` and assign them separately to `Jan`, `Mar` and `May`". The net result is `Jan == 0`, `Mar == 1`, and `May == 2`. You were getting an exception because there are more values in `range(1, 32)` than there are in `Jan, Mar, May, Jul, Aug, Oct, Dec`. The error message gives you a hint here (although I wish it mentioned the number of items on each side...) – Karl Knechtel Jun 01 '12 at 22:18
  • But...this assigns each month to the same, single list. What the OP probably wants is a separate new list for each month. – Nick Perkins Dec 27 '12 at 16:12
6

The easiest way to do what you described would be to use the x=y=z ... = VALUE syntax, where x, y, z, and any other variables you include will all be assigned the value of VALUE.

In your example, all the comma-separated variables on the left side of the equals sign are assigned to sequential values of a tuple on the right side. Hence, you could do something like this:

values = ( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec = values

In your code, you have 7 values on the left, and 31 on the right, so you get an out of range error because the list on the left is longer than the number or variables on the left side to be assigned the values in it. I know the code above doesn't have much relevance to achieving your goal, but I thought I'd at least give some insight as to what it was trying to do. :D

Gordon Gustafson
  • 40,133
  • 25
  • 115
  • 157
  • Thanks, that was exactly what I was wondering and makes perfect sense to my follow-up questions. So its fine to assign: a,b,c = range(3) – tijko Jun 01 '12 at 21:59