Python Forum

Full Version: Make one thing act like another
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a long list of 750 single digit numbers.

WorldList = [0,0,3,7,3,2...]
I use the variable valIndex plus a number to call certain points in the list.

WorldList[valIndex + 30] = 0
I know that I could use an if statement every time I call a point, but could I make something at the top of my code that says "anywhere in the code where valIndex is a multiple of 30, set WorldList[valIndex+30] to 0"

so if I set valIndex to 60, then WorldList at 90 acts like 0. But as soon as valIndex changes, WorldList[90] goes back to what it was originally.

I know this is confusing and probably doesn't make sense.
I would use a class with properties. Properties are attributes that allow you to set up code that is triggered when the attribute changes. So whenever valIndex changes, you could have code that automatically changes WordList as well, assuming they were both attributes of the class.
Do I understand correctly that you want:

- bind val_index name to value (object)
- calculate list index (val_index + 30) and change corresponding value in list to 0. Or do you need change all values to 0 where indices are i % 30 == 0
- do some magic
- change value in list back to original value

Question:

- can you use throwaway lists? Every time you just create new list, use it and discard? This way original list remains same