Python Forum
Why is my for loop failing? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Why is my for loop failing? (/thread-12761.html)



Why is my for loop failing? - microphone_head - Sep-11-2018

Can you tell me why my for loop isn't working?

I'm working on managing my input from my game-pad controller device.

I store most of the details in a list.
I was trying to reset/clear the button array section of the list that holds the button states.
I thought I could use "button_state =0" in a for loop, but its not working the way I hoped.

C_LIST_BUTTONS_ARRAY =4

#   This list represents data obtained from game-pad.
controller =[0, 0, 0, 0, [1, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0]]

print("Initial state of array.")
for button_index in range(len(controller[C_LIST_BUTTONS_ARRAY])):
    button_state =controller[C_LIST_BUTTONS_ARRAY][button_index]
    print(button_state, end ="")
# end for
print()
# ---------------------------------------------------------------------------


print("\n\nResults of for loop using in object")

#   Reset all the button states in the button array.
for button_state in controller[C_LIST_BUTTONS_ARRAY]:
    button_state =0
# end for
#   _.

for button_state in controller[C_LIST_BUTTONS_ARRAY]:
    print(button_state, end ="")
# end for
print()
# ---------------------------------------------------------------------------


print("\n\nResults of for loop using range.")

#   Reset all the button states in the button array.
for button_index in range(len(controller[C_LIST_BUTTONS_ARRAY])):
    controller[C_LIST_BUTTONS_ARRAY][button_index] =0
# end for
#   _.


for button_state in controller[C_LIST_BUTTONS_ARRAY]:
    print(button_state, end ="")
# end for
print()
Output:
Initial state of array. 10000000000 Results of for loop using in object 10000000000 Results of for loop using range. 00000000000



RE: Why is my for loop failing? - Windspar - Sep-11-2018

button_state point to non ref value. List, dict, and classes are ref value.
C_LIST_BUTTONS_ARRAY = 4
controller =[0, 0, 0, 0, [1, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0]]

print(controller[C_LIST_BUTTONS_ARRAY])
controller[C_LIST_BUTTONS_ARRAY] = [0 for i in range(len(controller[C_LIST_BUTTONS_ARRAY]))]
print(controller[C_LIST_BUTTONS_ARRAY])

When iterating over a list. It return list position value address. Not controller[4][0].
Python only create same value once in memory. All variables just point to objects memory.

C_LIST_BUTTONS_ARRAY = 4
controller =[0, 0, 0, 0, [1, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0]]

# list object address
print(id(controller[C_LIST_BUTTONS_ARRAY]))
for button_state in controller[C_LIST_BUTTONS_ARRAY]:
    print(id(button_state)) # value address



RE: Why is my for loop failing? - microphone_head - Sep-11-2018

I'm not sure what you mean. Now you got me thinking I don't know line below is doing? Huh

button_state =0



RE: Why is my for loop failing? - Windspar - Sep-11-2018

button_state = 0 is just assigning local value
class A:
    def __init__(self):
        self.a = 1

    def __repr__(self):
        return 'A:' + str(self.__dict__)

a = [0, A(), [1]]

for item in a:
    print(type(item))
    if isinstance(item, A):
        # changing class value
        item.a = 2
    elif isinstance(item, list):
        # changing list value
        item[0] = 5
    else:
        # assigning local value
        item = 1

print(a)



RE: Why is my for loop failing? - microphone_head - Sep-11-2018

That's a good example,

Thanks.