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

Discussion in 'Python' started by Chris Rebert, Aug 15, 2012.

  1. Chris Rebert

    Chris Rebert Guest

    On Tue, Aug 14, 2012 at 10:07 PM, levi nie <> 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_(computer_science)#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
     
    Chris Rebert, Aug 15, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Amir
    Replies:
    3
    Views:
    624
  2. nc
    Replies:
    1
    Views:
    529
    nice.guy.nige
    Feb 3, 2005
  3. Replies:
    2
    Views:
    371
  4. Jeannie
    Replies:
    15
    Views:
    916
    Jeannie
    Aug 30, 2005
  5. =?Utf-8?B?TFc=?=
    Replies:
    8
    Views:
    447
    =?Utf-8?B?TFc=?=
    Apr 25, 2007
Loading...

Share This Page