Re: [a,b,c,d] = 1,2,3,4

2015-08-26 Thread Terry Reedy

On 8/26/2015 8:21 AM, Tim Chase wrote:


a, b, c = (x for x in range(3)) # a generator for instance


Since range() *is* a generator, why not just use


In Python 3, range is a sequence class with a separate iterator class

>>> r = range(3)
>>> r
range(0, 3)
>>> iter(r)


Like all sequences, a range object can be iterated multiple times as a 
new iterator is used each time.


>>> list(r)
[0, 1, 2]
>>> list(r)
[0, 1, 2]

--
Terry Jan Reedy

--
https://mail.python.org/mailman/listinfo/python-list


Re: [a,b,c,d] = 1,2,3,4

2015-08-26 Thread Jean-Michel Pichavant
- Original Message -
> From: "Chris Angelico" 
> Cc: python-list@python.org
> Sent: Wednesday, 26 August, 2015 3:04:05 PM
> Subject: Re: [a,b,c,d] = 1,2,3,4
> 
> On Wed, Aug 26, 2015 at 12:59 AM, Jean-Michel Pichavant
>  wrote:
> > 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
> 
> FWIW, a generator is not a sequence; this works because the right
> side
> can be any *iterable*, even more general than sequences.
> 
> ChrisA

Sorry about that, I've been mislead by the tutorial which uses sequence where 
it should have used iterable, so I though they where the same but they're not.

https://docs.python.org/2/library/stdtypes.html
"There are seven sequence types: strings, Unicode strings, lists, tuples, 
bytearrays, buffers, and xrange objects."

I stand corrected.

JM


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be 
privileged. If you are not the intended recipient, please notify the sender 
immediately and do not disclose the contents to any other person, use it for 
any purpose, or store or copy the information in any medium. Thank you.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [a,b,c,d] = 1,2,3,4

2015-08-26 Thread Chris Angelico
On Wed, Aug 26, 2015 at 12:59 AM, Jean-Michel Pichavant
 wrote:
> 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

FWIW, a generator is not a sequence; this works because the right side
can be any *iterable*, even more general than sequences.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [a,b,c,d] = 1,2,3,4

2015-08-26 Thread Tim Chase
On 2015-08-25 16:59, Jean-Michel Pichavant wrote:
> - Original Message -
> > From: "Joel Goldstick" 
> > 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



-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [a,b,c,d] = 1,2,3,4

2015-08-26 Thread Jean-Michel Pichavant
- Original Message -
> From: "Joel Goldstick" 
> 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

That would be a generator unpacking combined with a tuple packing (is packing 
restricted to tuples and lists ? probably)

JM


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be 
privileged. If you are not the intended recipient, please notify the sender 
immediately and do not disclose the contents to any other person, use it for 
any purpose, or store or copy the information in any medium. Thank you.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [a,b,c,d] = 1,2,3,4

2015-08-25 Thread Jussi Piitulainen
Ian Kelly writes:
> On Tue, Aug 25, 2015 at 9:32 AM, Skip Montanaro wrote:
>> On Tue, Aug 25, 2015 at 10:24 AM, Jussi Piitulainen wrote:
>>>
>>> When I try it today, round brackets also work, both in 2.6.6 and
>>> 3.4.0 - no idea what version it was where they failed or if I'm
>>> imagining the whole thing.
>>
>> You are imagining the whole thing. Either that, or you had some other
>> problem with your tuple unpacking which kept it from working. That
>> has been a part of the language as far back as I can remember. I
>> started using Python around the 1.0 timeframe.
>
> My guess is that Jussi was trying to unpack a sequence of a single
> element like this:
>
> (a) = some_sequence
[snip]

It wasn't that. It was a known number of tab-separated fields that I
wanted to name individually, like this:

ID, FORM, LEMMA, POS, MOR, FEATS, \
HEAD, REL, DEPS, MISC \
= line.split('\t')

That's from actual code but not necessarily from the place where I first
tried and failed to use parentheses. It didn't occur to me to try square
brackets, so I got in the habit of using backslashes as above. (That
script is dated in January 2015. The something that happened happened
some months before that. But it may have been a hallucination.)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [a,b,c,d] = 1,2,3,4

2015-08-25 Thread Jussi Piitulainen
Skip Montanaro  writes:

> On Tue, Aug 25, 2015 at 10:24 AM, Jussi Piitulainen wrote:
>
>> When I try it today, round brackets also work, both in 2.6.6 and
>> 3.4.0 - no idea what version it was where they failed or if I'm
>> imagining the whole thing.
>
> You are imagining the whole thing. Either that, or you had some other
> problem with your tuple unpacking which kept it from working. That has
> been a part of the language as far back as I can remember. I started
> using Python around the 1.0 timeframe.

At least they work now, and clearly are intended to work. But I just
checked and I have scripts with backslash-terminated lines where I'm
sure I wanted to use those brackets. Could it have been some sort of
temporary regression? Well, never mind :)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [a,b,c,d] = 1,2,3,4

2015-08-25 Thread Ian Kelly
On Tue, Aug 25, 2015 at 9:32 AM, Skip Montanaro
 wrote:
>
> On Tue, Aug 25, 2015 at 10:24 AM, Jussi Piitulainen
>  wrote:
>>
>> When I try it today, round brackets
>> also work, both in 2.6.6 and 3.4.0 - no idea what version it was where
>> they failed or if I'm imagining the whole thing.
>
>
> You are imagining the whole thing. Either that, or you had some other
> problem with your tuple unpacking which kept it from working. That has been
> a part of the language as far back as I can remember. I started using Python
> around the 1.0 timeframe.

My guess is that Jussi was trying to unpack a sequence of a single
element like this:

(a) = some_sequence

With the result that a is assigned the whole sequence instead of the
one element, because (a) does not denote a tuple, but merely an
individual parenthesized expression. Any of these would work in its
place:

(a,) = some_sequence
a, = some_sequence
[a] = some_sequence
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [a,b,c,d] = 1,2,3,4

2015-08-25 Thread Skip Montanaro
On Tue, Aug 25, 2015 at 10:24 AM, Jussi Piitulainen <
harvested.address@is.invalid> wrote:

> When I try it today, round brackets
> also work, both in 2.6.6 and 3.4.0 - no idea what version it was where
> they failed or if I'm imagining the whole thing.
>

You are imagining the whole thing. Either that, or you had some other
problem with your tuple unpacking which kept it from working. That has been
a part of the language as far back as I can remember. I started using
Python around the 1.0 timeframe.

Skip
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [a,b,c,d] = 1,2,3,4

2015-08-25 Thread Jussi Piitulainen
"ast" writes:

>>>> [a,b,c,d] = 1,2,3,4
>>>> a
> 1
>>>> b
> 2
>>>> c
> 3
>>>> d
> 4
>
> I have never seen this syntax before. Is it documented.
> Is there a name for that ?

I remember being unhappy when a similar assignment with round brackets
turned out to be invalid syntax. Then I learned (in this newsgroup) that
square brackets would work, and so they did, and then I was happy again,
though it felt a bit inconsistent. When I try it today, round brackets
also work, both in 2.6.6 and 3.4.0 - no idea what version it was where
they failed or if I'm imagining the whole thing.

The page that Joel Goldstick cited claims that Guido van Rossum himself
has called the left side of a statement like "w,x,y,z = t3" (no brackets
on that page) "the list of variables" (but their link to the evidence is
dead).
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [a,b,c,d] = 1,2,3,4

2015-08-25 Thread ast


"Joel Goldstick"  a écrit dans le message de 
news:mailman.27.1440515128.11709.python-l...@python.org...

On Tue, Aug 25, 2015 at 10:32 AM, Cody Piersall  wrote:



On Tue, Aug 25, 2015 at 9:16 AM, ast  wrote:




The original example is one I haven't seen in the wild.


I found it using matplotlib

import numpy as np
import matplotlib.pyplot as plt

f, (ax1, ax2) = plt.subplots(2, 1, sharex=True)




--
https://mail.python.org/mailman/listinfo/python-list


Re: [a,b,c,d] = 1,2,3,4

2015-08-25 Thread ast


"ast"  a écrit dans le message de 
news:55dc853c$0$3083$426a7...@news.free.fr...


"Joel Goldstick"  a écrit dans le message de 
news:mailman.23.1440513059.11709.python-l...@python.org...

On Tue, Aug 25, 2015 at 10:16 AM, ast  wrote:

[a,b,c,d] = 1,2,3,4
a


1


b


2


c


3


d


4

I have never seen this syntax before. Is it documented.
Is there a name for that ?

thx
--
https://mail.python.org/mailman/listinfo/python-list


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





http://www.developer.com/lang/other/article.php/630101/Learn-to-Program-using-Python-Unpacking-Tuples.htm


--
Joel Goldstick
http://joelgoldstick.com


Yes you are right, it is related to tuple unpacking
Here are some useful examples I found.
http://svn.python.org/projects/python/branches/pep-0384/Lib/test/test_unpack_ex.py


Unpack in list

   >>> [a, *b, c] = range(5)
   >>> a == 0 and b == [1, 2, 3] and c == 4
   True 


--
https://mail.python.org/mailman/listinfo/python-list


Re: [a,b,c,d] = 1,2,3,4

2015-08-25 Thread ast


"Joel Goldstick"  a écrit dans le message de 
news:mailman.23.1440513059.11709.python-l...@python.org...

On Tue, Aug 25, 2015 at 10:16 AM, ast  wrote:

[a,b,c,d] = 1,2,3,4
a


1


b


2


c


3


d


4

I have never seen this syntax before. Is it documented.
Is there a name for that ?

thx
--
https://mail.python.org/mailman/listinfo/python-list


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





http://www.developer.com/lang/other/article.php/630101/Learn-to-Program-using-Python-Unpacking-Tuples.htm


--
Joel Goldstick
http://joelgoldstick.com


Yes you are right, it is related to tuple unpacking
Here are some useful examples I found.
http://svn.python.org/projects/python/branches/pep-0384/Lib/test/test_unpack_ex.py 


--
https://mail.python.org/mailman/listinfo/python-list


Re: [a,b,c,d] = 1,2,3,4

2015-08-25 Thread Joel Goldstick
On Tue, Aug 25, 2015 at 10:32 AM, Cody Piersall  wrote:
>
>
> On Tue, Aug 25, 2015 at 9:16 AM, ast  wrote:
>>>>>
>>>>> [a,b,c,d] = 1,2,3,4
>>>>> a
>>
>> 1
>>>>>
>>>>> b
>>
>> 2
>>>>>
>>>>> c
>>
>> 3
>>>>>
>>>>> d
>>
>> 4
>>
>> I have never seen this syntax before. Is it documented.
>> Is there a name for that ?
>>
>> thx
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>
>
> That's called "sequence unpacking"
>
> Cody
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>

The original example is one I haven't seen in the wild.  One nifty use
of this feature is to exchange values like this:

a, b = b, a

it saves the use of a temporary name
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [a,b,c,d] = 1,2,3,4

2015-08-25 Thread Jean-Michel Pichavant
- Original Message -
> From: "ast" 
> To: python-list@python.org
> Sent: Tuesday, 25 August, 2015 4:16:17 PM
> Subject: [a,b,c,d] = 1,2,3,4
> 
> >>> [a,b,c,d] = 1,2,3,4
> >>> a
> 1
> >>> b
> 2
> >>> c
> 3
> >>> d
> 4
> 
> I have never seen this syntax before. Is it documented.
> Is there a name for that ?
> 
> thx

You probably have already seen something like:

a,b,c,d = 1,2,3,4

which is the same code than yours with the list replaced by a tuple.

Moreover:
https://docs.python.org/2/tutorial/datastructures.html

"""
x, y, z = t
This is called, appropriately enough, sequence unpacking and works for any 
sequence on the right-hand side. Sequence unpacking requires the list of 
variables on the left to have the same number of elements as the length of the 
sequence. Note that multiple assignment is really just a combination of tuple 
packing and sequence unpacking.
"""

It's slightly confusing because it mentions a "list of variable" and then a 
"tuple packing" while the example uses a tuple.
Fortunately, lists and tuples can be used in both cases.

JM


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be 
privileged. If you are not the intended recipient, please notify the sender 
immediately and do not disclose the contents to any other person, use it for 
any purpose, or store or copy the information in any medium. Thank you.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [a,b,c,d] = 1,2,3,4

2015-08-25 Thread Cody Piersall
On Tue, Aug 25, 2015 at 9:16 AM, ast  wrote:

> [a,b,c,d] = 1,2,3,4
>>>> a
>>>>
>>> 1
>
>> b
>>>>
>>> 2
>
>> c
>>>>
>>> 3
>
>> d
>>>>
>>> 4
>
> I have never seen this syntax before. Is it documented.
> Is there a name for that ?
>
> thx
> --
> https://mail.python.org/mailman/listinfo/python-list
>

That's called "sequence unpacking"

Cody
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [a,b,c,d] = 1,2,3,4

2015-08-25 Thread Joel Goldstick
On Tue, Aug 25, 2015 at 10:16 AM, ast  wrote:
>>>> [a,b,c,d] = 1,2,3,4
>>>> a
>
> 1
>>>>
>>>> b
>
> 2
>>>>
>>>> c
>
> 3
>>>>
>>>> d
>
> 4
>
> I have never seen this syntax before. Is it documented.
> Is there a name for that ?
>
> thx
> --
> https://mail.python.org/mailman/listinfo/python-list

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
>>>


http://www.developer.com/lang/other/article.php/630101/Learn-to-Program-using-Python-Unpacking-Tuples.htm


-- 
Joel Goldstick
http://joelgoldstick.com
-- 
https://mail.python.org/mailman/listinfo/python-list


[a,b,c,d] = 1,2,3,4

2015-08-25 Thread ast

[a,b,c,d] = 1,2,3,4
a

1

b

2

c

3

d

4

I have never seen this syntax before. Is it documented.
Is there a name for that ?

thx
--
https://mail.python.org/mailman/listinfo/python-list