Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #27080

Re: it's really strange.how does it work?

References <CAEMsKDuzVPN1_k0n3NpBA=XZ340BLPmReMNozKk7O9-hY8+GEQ@mail.gmail.com>
Date 2012-08-14 22:24 -0700
Subject Re: it's really strange.how does it work?
From Chris Rebert <clp2@rebertia.com>
Newsgroups comp.lang.python
Message-ID <mailman.3301.1345008246.4697.python-list@python.org> (permalink)

Show all headers | View raw


On Tue, Aug 14, 2012 at 10:07 PM, levi nie <levinie001@gmail.com> wrote:
> ok,what does "start, stop = 0, start" in the code mean?
> it's really strange.how does it work?

It's just parallel assignment
(http://en.wikipedia.org/wiki/Assignment_%28computer_science%29#Parallel_assignment
).

As to exactly how it works:
http://docs.python.org/reference/simple_stmts.html#assignment-statements :
"If the target [of the assignment] is a comma-separated list: The
[value being stored] must be an iterable with the same number of items
as there are targets in the target list, and the items are assigned,
from left to right, to the corresponding targets." [not a completely
direct quote]

Tuples are iterable (e.g. we can write `for item in some_tuple:`; in
laymen's terms, it's similar to being a sequence). Recall that commas,
and not parentheses, are what create tuples according to Python's
syntax:
$ python
Python 2.7.2 (default, Jun 20 2012, 16:23:33)
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 1,2
>>> x
(1, 2)
>>> type(x)
<type 'tuple'>
>>>

So, your code snippet creates an anonymous temporary tuple of length 2
[i.e. (0, start) ], and the assignment then unpacks that tuple into
the 2 respective variables.

Cheers,
Chris

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: it's really strange.how does it work? Chris Rebert <clp2@rebertia.com> - 2012-08-14 22:24 -0700

csiph-web