update Adding a usage example to better explain why I'm trying to elicit this behavior. See update at end of post. Added better syntax for links and added a link to article suggested as an answer.
I am trying to use an iterator to alter the value of a list of variables.
>>> a = 1
>>> b = 2
>>> c = 3
>>> for i in [a,b,c]:
i += 1
>>> a,b,c #Expecting (2, 3, 4)
(1, 2, 3)
This doesn't appear to work, I've tried some other ways (see below) without success.
- Will someone please tell me a better way to approach this problem?
- Will someone explain why the example above doesn't work as I expected?
- Will someone tell me how/where I could have found #2 in python help documentation?
Places I've previously looked for answers...
StackOverflow question on reassigning variables (but not a list of variables)
Python docs: Objects and Value Types
I feel like the last reference to the python docs might have the answer, but I was overwhelmed with the amount of info, my brain is tired, and I'm hoping someone on s.o. can help.
also tried...
>>> help(__name__)
Help on module __main__:
NAME
__main__
DATA
__annotations__ = {}
a = 1
b = 2
c = 3
i = 4
FILE
(built-in)
but if anything, this only confused me further.
Lastly I tried...
>>> a = 1
>>> b = 2
>>> c = 3
>>> R = [a, b, c]
>>> for i in range(3):
R[i] += 1
>>> a, b, c #Hoping for (2, 3, 4)
(1, 2, 3)
>>> R #Kind of expected (1, 2, 3) based on the above behavior
[2, 3, 4]
update I used the list for convenience, since I could iterate through its members. The part I'm not understanding is that when I say...
>>> x = [a, b, c]
I am creating a list such that
x = [the value assigned to a,
the value assigned to b,
the value assigned to c]
rather than
x = [the variable a, the variable b, the variable c]
so when I am trying to use the syntax
>>> x[0] += 1 #take the current ITEM in place 0 and increment its value by 1.
instead of
>>> a += 1
it is instead interpreted as... take the current VALUE of the ITEM in place 0, increment that VALUE by 1, this is the new VALUE of ITEM in place 0 ... and I lose the reference to the original ITEM... the variable a.
Here is a usage example of why I am trying to elicit this behavior...
>>> class breakfast():
>>> def __init__(self, eggs=None, bacon=None, spam=None):
>>> self.eggs = eggs
>>> self.bacon = bacon
>>> self.spam = spam
>>> updateOrder = False
>>> updateItems = []
>>> for i in [self.eggs, self.bacon, self.spam]:
>>> if i == None:
>>> updateOrder = True
>>> updateItems.append(i)
>>> else:
>>> pass
>>> if updateOrder:
>>> self.updateOrder(itemsToUpdate = updateItems)
>>>
>>> def updateOrder(self, itemsToUpdate):
>>> for i in itemsToUpdate: ###also tried...
>>> ###for i in range(len(itemsToUpdate)):
>>> print('You can\'t order None, so I am updating your order to 0 for some of your items.')
>>> i = 0
>>> ###itemsToUpdate[i] = 0
>>>
>>> #Finally, the good part, behavior I'm after...
>>> myBreakfast = breakfast(eggs=2,bacon=3)
You can't order None, so I am updating your order to 0 for some of your items.
>>> myBreakfast.spam == 0 #hoping for True....
False
The only way I know would work to get this behavior is to instead say...
>>> ...
>>> def updateOrder(self):
>>> orderUpdated=False
>>> if self.eggs == None:
>>> self.eggs = 0
>>> orderUpdated = True
>>> if self.bacon == None:
>>> self.bacon = 0
>>> orderUpdated = True
>>> if self.spam == None:
>>> self.spam = 0
>>> orderUpdated = True
>>> if orderUpdated:
>>> print('You can\'t order None, so I am updating your order')
However, if there are (lots) more than just 3 items on the menu the code for updateOrder would become very long and worse repetitive.