You take out the value, create a new value (by adding to the original one or doing some other operation), destroy the old one, and put the new one back into the variable “box”. When you change a variable based on its current state, the same thing happens. When you replace value of a variable, you take out the old value, destroy it (by throwing it into a nearest black hole, I assume), create a new one, and put it into the variable “box”. When you use this value or assign it to a different variable, you can assume that Python makes a copy of it 53 and puts that copy into a different variable “box”. In short, a variable can be thought of as a “box” with a variable name written on it and a value being stored “inside”. You may remember the variable-as-a-box metaphor that I used to introduce variables. You need to learn about this distinction as these two kinds of objects (values) behave very differently under some circumstances, which is both good (power!) and bad (weird unexpected behavior!) news. These are mutable, like lists in contrast to “normal” immutable values (integers, floats, strings). 10.2 Variables as boxes (immutable objects)
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |