ValueError vs IndexError, unpacking arguments with string.split

Morten W. Petersen morphex at gmail.com
Fri Nov 30 18:55:15 EST 2018


On Sat, Dec 1, 2018 at 12:25 AM Chris Angelico <rosuav at gmail.com> wrote:

> On Sat, Dec 1, 2018 at 10:18 AM Morten W. Petersen <morphex at gmail.com>
> wrote:
> >
> > On Fri, Nov 30, 2018 at 7:25 PM Dan Sommers <
> > 2QdxY4RzWzUUiLuE at potatochowder.com> wrote:
> >
> > > On 11/30/18 12:00 PM, Morten W. Petersen wrote:
> > >
> > >  > I guess syntax could be added, so that
> > >  >
> > >  > a, b, @c = some sequence
> > >  >
> > >  > would initialize a and b, and leave anything remaining in c.  We
> could
> > >  > then call this @ syntax "teh snek".
> > >
> > > Close.  ;-)  Try this:
> > >
> > >      a, b, *c = [4, 5, 6, 7]
> > >
> >
> >
> >
> > I did not know that.  Or maybe I read it somewhere and it was in the
> > subconscious.
> >
>
> Be aware that this isn't the same as "leaving" everything in c. It
> will specifically gather everything in the rest of the iterable and
> put it all into a list in c. For this example, that wouldn't be a
> problem, but consider:
>
> >>> x = range(10)
> >>> a, b, *c = x
> >>> print(x)
> range(0, 10)
> >>> print(a, b, c)
> 0 1 [2, 3, 4, 5, 6, 7, 8, 9]
>
> But as long as you never dive into infinite iterators, it's going to be
> fine.
>


Mm, yes.  This was the intuitive understanding I had of it.

The range in Python 3 is what xrange was in Python 2, and
if I do

x=list(range(100000000))

it takes quite a bit of time, as opposed to

x = range(100000000)

But this raises the question of how to write Python code,
short and sweet, that could handle infinite iterators in
such an unpack with multiple variables to assign to.

Which I guess is mostly theoretical, as there are other
ways of designing code to avoid needing to unpack
an infinite iterator using the * prefix.

-Morten

-- 
Videos at https://www.youtube.com/user/TheBlogologue
Twittering at http://twitter.com/blogologue
Blogging at http://blogologue.com
Playing music at https://soundcloud.com/morten-w-petersen
Also playing music and podcasting here:
http://www.mixcloud.com/morten-w-petersen/
On Google+ here https://plus.google.com/107781930037068750156
On Instagram at https://instagram.com/morphexx/



More information about the Python-list mailing list