Go Back   Velocity Reviews > Newsgroups > Python
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Python - variable length tuple assignment

 
Thread Tools Search this Thread
Old 02-25-2009, 09:16 AM   #1
Default variable length tuple assignment


Sorry if this is too simple but I couldn't find.

I vaguely remember there is a means to assign a variable length tuple
and catch the 'rest' like

S="a,b,c,d"

(A,B,<list of remaining items>) = S.split(',')

I know I could do
SL= split(',')
(A,B)=SL[:2]
Rest= SL[2:]

but is there some shorthand for this?

Many thanks for a hint,

Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany


Helmut Jarausch
  Reply With Quote
Old 02-25-2009, 09:41 AM   #2
Chris Rebert
 
Posts: n/a
Default Re: variable length tuple assignment
On Wed, Feb 25, 2009 at 1:16 AM, Helmut Jarausch
<> wrote:
> Sorry if this is too simple but I couldn't find.
>
> I vaguely remember there is a means to assign a variable length tuple
> and catch the 'rest' Â*like
>
> S="a,b,c,d"
>
> (A,B,<list of remaining items>) = S.split(',')


In Python 3.0 (IIRC):

A, B, *rest = S.split(',')

Cheers,
Chris

--
Follow the path of the Iguana...
http://rebertia.com


Chris Rebert
  Reply With Quote
Old 02-25-2009, 11:35 AM   #3
Tim Chase
 
Posts: n/a
Default Re: variable length tuple assignment
Chris Rebert wrote:
> On Wed, Feb 25, 2009 at 1:16 AM, Helmut Jarausch
> <> wrote:
>> Sorry if this is too simple but I couldn't find.
>>
>> I vaguely remember there is a means to assign a variable length tuple
>> and catch the 'rest' like
>>
>> S="a,b,c,d"
>>
>> (A,B,<list of remaining items>) = S.split(',')

>
> In Python 3.0 (IIRC):
>
> A, B, *rest = S.split(',')


As an aside, as of the last time I read the PEP[1] on this, I
believe it exhausts (or attempts to exhaust) any iterator. IMHO,
I think this exhausting is a bad idea because it prevents things like

def numbers(start=0):
i = start
while True:
yield i
i += 1

CONST_A, CONST_B, CONST_C, *rest = numbers()

which will hang in current Py3.0 until you blow a stack or
overrun your heap somewhere because it will try to exhaust the
infinite loop. It also changes the type from iter() to list()
for the remaining content (not as grevious).


I agree that the "internal star" usage needs to exhaust the iterator:

A, *rest, C, D = s.split(',')

but the terminal-star syntax should be a little more gracious
with iterators.

Anyways, my $0.02 (minus taxes, money for multiple bailouts, and
deflated by economic conditions -- so not worth much .

-tkc

[1]
http://www.python.org/dev/peps/pep-3132/








Tim Chase
  Reply With Quote
Old 02-25-2009, 11:08 PM   #4
Steven D'Aprano
 
Posts: n/a
Default Re: variable length tuple assignment
Tim Chase wrote:

> As an aside, as of the last time I read the PEP[1] on this, I
> believe it exhausts (or attempts to exhaust) any iterator. IMHO,
> I think this exhausting is a bad idea because it prevents things like
>
> def numbers(start=0):
> i = start
> while True:
> yield i
> i += 1
>
> CONST_A, CONST_B, CONST_C, *rest = numbers()
>
> which will hang in current Py3.0 until you blow a stack or
> overrun your heap somewhere because it will try to exhaust the
> infinite loop.


But what else can it do? Do you expect Python to read your mind and
magically know when you intend to use rest and when you're intending to
just throw it away?

Perhaps Python could do that, via static analysis -- if rest is never used
again, don't bother exhausting the iterator. But that will lead to
differences in iterators that have side-effects. It will also have a subtle
difference in behaviour here:

it = xrange(5) # for example
a, b, c, *rest = it
L = list(it) # L is now [3, 4]

versus

a, b, c, *rest = xrange(5)
parrot(rest)
L = list(it) # L is now []


> It also changes the type from iter() to list()
> for the remaining content (not as grevious).


But that's what unpacking does. It would be a major semantic change for

x, *s = some_iterator

to make s an alias of some_iterator. And why would you want it to? If you
do, just do this:

x, s = some_iterator.next(), some_iterator



--
Steven



Steven D'Aprano
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing value with out using variable in query string in PHP! Ali_ggl General Help Related Topics 0 11-29-2008 12:22 PM
Variable Scope in asp.Net jansi_rk Software 1 09-18-2006 06:05 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46