Python Forum
Make one thing act like another - 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: Make one thing act like another (/thread-12245.html)



Make one thing act like another - Zman350x - Aug-15-2018

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.


RE: Make one thing act like another - ichabod801 - Aug-16-2018

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.


RE: Make one thing act like another - perfringo - Aug-16-2018

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