Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Setting For Loop Counter
#3
A range is an immutable sequence. It cannot be changed. Use a while loop if you don't know how many times the loop needs to execute.

The way you are attempting to change range makes me think you don't understand how Python variables work either. A python variable is a lot like a key in a python dictionary. It is only a name that is used to reference the value, not the value itself.

In the code below I make my own range-like iterator. I use variables to reference the start, stop and step values. After I "initialize" the range-like iterator I try to mess with the stop and step, but the changes have no effect. Why is that? Is range-like also an immutable sequence?
def range_like(start, stop, step=1):
    index = start
    while index < stop:
        yield index
        index += step

start = 0
stop = 5
step = 1
for x in range_like(start, stop, step):
    stop = stop - 1
    step = step + 1
    print("x")
Output:
0 1 2 3 4
To test that hypothesis, I modify range-like to accept arguments as a list. While iterating I modify the values in the list and see if this changes how range-like operates.
def range_like(args):
    index = args[0]
    while index < args[1]:
        yield index
        index += args[2]

args = [0, 5, 1]
for x in range_like(args):
    args[1] -= 1
    args[2] += 1
    print(x)
Output:
0 2
This time changing the stop and step arguments changes the sequence.

The reason the two examples work differently is that in the first example I never changed the stop and step arguments. I reassign the stop and step variables, but range_like() does not use the stop and step variables. When I called range_like(start, stop, step) at the top of the for loop, I passed the objects referenced by these variables. Instead of stop, I passed 5. Instead of step I passed 1. Reassigning the variable names to reference different objects has no effect on the objects themselves.

In the second example I passed a list object. When I made changes to args inside the for loop I was actually modifying the list referenced by the "args" variable. Since the for loop and the range_like generator were both using the same list object, changes to the list changed the sequence produced by the generator.

To sum up:
Variables are names that reference objects. They are not the objects themself

Re-assigning a variable does not change the object previously referenced by the variable.

When you use a variable in a function argument list, you are not passing the variable to the function, you are passing the object referenced by the variable.
Reply


Messages In This Thread
Setting For Loop Counter - by rturus - Dec-07-2022, 12:21 PM
RE: Setting For Loop Counter - by DeaD_EyE - Dec-07-2022, 12:49 PM
RE: Setting For Loop Counter - by deanhystad - Dec-07-2022, 01:34 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Setting maximum value of Range() in For loop pmt 4 2,624 Aug-04-2019, 02:38 PM
Last Post: pmt
Question [Help] How to end While Loop using counter? {Screenshot attached} vanicci 2 3,106 Aug-02-2018, 10:09 PM
Last Post: vanicci
  [Tkinter] Loop Counter for OptionMenu Creation JP_ROMANO 4 5,155 Sep-29-2017, 06:29 PM
Last Post: JP_ROMANO

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020