Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #95648 > unrolled thread
| Started by | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| First post | 2015-08-26 07:21 -0500 |
| Last post | 2015-08-26 07:21 -0500 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: [a,b,c,d] = 1,2,3,4 Tim Chase <python.list@tim.thechases.com> - 2015-08-26 07:21 -0500
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2015-08-26 07:21 -0500 |
| Subject | Re: [a,b,c,d] = 1,2,3,4 |
| Message-ID | <mailman.42.1440593198.11709.python-list@python.org> |
On 2015-08-25 16:59, Jean-Michel Pichavant wrote:
> ----- Original Message -----
> > From: "Joel Goldstick" <joel.goldstick@gmail.com>
> > its called list unpacking or packing (?)
> >
> > the right side is considered a tuple because of the commas
> > >>> a = 1,2,3
> > >>> a
> > (1, 2, 3)
> > >>> a[1]
> > 2
>
> To add to Joel's answer, the right side can be *any* sequence, and
> is not restricted to lists or tuples.
>
> a, b, c = (x for x in range(3)) # a generator for instance
Since range() *is* a generator, why not just use
a, b, c = range(3)
I do this often for setting constants:
(
HR_FILE,
PHONE_FILE,
COST_CENTERS_FILE,
) = range(3)
however I have to keep track of how many entries are in there. When
Py3 introduced variable tuple unpacking, I'd hoped the last one
wouldn't consume generators, allowing me to do something like
(
HR_FILE,
PHONE_FILE,
COST_CENTERS,
*_
) = itertools.count()
so I could insert additional constants and have the list
automatically adjust. Alas, no such joy. The new Enum class does
offer an auto-number functionality, but it's clunky, IMHO.
https://docs.python.org/3/library/enum.html#autonumber
-tkc
Back to top | Article view | comp.lang.python
csiph-web