[Tutor] Java style assignments in Python

Zak Arntson zak at harlekin-maus.com
Wed Sep 10 11:40:39 EDT 2003


> hello all, I was curious if there is a way to implement Java style
> assignments in Python.  Let's say I have a block of code:
>
> list_A = []
> list_B = []
> list_C = []
>
> and I want to condense it to:
>
> list_A, list_B, list_C = []

First up, don't do this:

>>> list_A, list_B, list_C = [[]] * 3

Because you'll get three variables referencing the same list.

It's not pretty, but you can do this:

>>> list_A, list_B, list_C = [[] for i in range(3)]

This creates a unique empty list for each variable. Of course, you'll have
to change the range parameter depending on how many variables you're
assigning.

My question for you is: Why do you want to do this? What kind of problem
are you programming for?

---
Zak Arntson
www.harlekin-maus.com - Games - Lots of 'em



More information about the Tutor mailing list