Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

it's really strange.how does it work?

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


levinie001 at gmail

Aug 14, 2012, 10:07 PM

Post #1 of 3 (112 views)
Permalink
it's really strange.how does it work?

ok,what does "start, stop = 0, start" in the code mean?
it's really strange.how does it work?

code:
def interval(start, stop=None, step=1):
'Imitates range() for step > 0'
if stop is None:
start, stop = 0, start
result = []
i = start
while i < stop:
result.append(i)
i += step
return result

print interval(10)


maniandram01 at gmail

Aug 14, 2012, 10:18 PM

Post #2 of 3 (108 views)
Permalink
Re: it's really strange.how does it work? [In reply to]

Even I got confused a bit:
explaination: this is called iterable unpacking
`start, stop = 0, start` is the same as:
temp = start
start = 0
stop = temp

On 15 August 2012 10:37, levi nie <levinie001 [at] gmail> wrote:

> ok,what does "start, stop = 0, start" in the code mean?
> it's really strange.how does it work?
>
> code:
> def interval(start, stop=None, step=1):
> 'Imitates range() for step > 0'
> if stop is None:
> start, stop = 0, start
> result = []
> i = start
> while i < stop:
> result.append(i)
> i += step
> return result
>
> print interval(10)
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>


clp2 at rebertia

Aug 14, 2012, 10:24 PM

Post #3 of 3 (108 views)
Permalink
Re: it's really strange.how does it work? [In reply to]

On Tue, Aug 14, 2012 at 10:07 PM, levi nie <levinie001 [at] gmail> 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
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.