Link to home
Start Free TrialLog in
Avatar of alexmac05
alexmac05Flag for United States of America

asked on

python code question - what is this line in my code doing and why?

this chunk of code is coming from a book and the book doesn't explain it and i am not getting it. The line start, stop=0, start has the result of switching the values for start and stop. But I don't understand why that is happening.

def interval(start, stop=None, step=1):
    print('start ' + str(start))
    print('stop ' + str(stop))
    print('step ' + str(step))
    print('*****')
    if stop is None:
        start, stop = 0, start
        print('start ' + str(start))
        print('stop ' + str(stop))
        print('step ' + str(step))

print(interval(10))

OUTPUT:
start 10
stop None
step 1
*****
start 0
stop 10
step 1
Avatar of Stephen Calabrese
Stephen Calabrese

def interval(start, stop=None, step=1): assigns the default value of stop variable to be None and the step varaible to be 1.

 if stop is None:
        start, stop = 0, start
        print('start ' + str(start))
        print('stop ' + str(stop))
        print('step ' + str(step))

Open in new window

assigns start to 0. Then stop gets assigned to the old value of start which is 10.
Avatar of alexmac05

ASKER

I do not understand this line.

start, stop = 0, start

what is this syntax? why is this setting start = 10?

I can understand stop = 0 but what are the two starts doing there? I can see the effect is that start = 10 but I don't understand how that happens.
why is this shuffling the parameters?

start, stop = 0, start # shuffle the parameters
I think I found the answer on stack overflow. reading it now

http://stackoverflow.com/questions/18165296/shuffling-parameters
ASKER CERTIFIED SOLUTION
Avatar of alexmac05
alexmac05
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
tags for this question:
shuffling parameters python
multiple assignment
Magnus Lie Hetland Python for Beginners
chapter 5
the solution was found by me on stackoverflow but the problem is hard to find in search engines so we should leave this for others.

I'm going to add some more tags
shuffling parameters python
multiple assignment
Magnus Lie Hetland Python for Beginners
chapter 5