How do you redefine a variable in a Python program?In python, a variable is just a name. It is the value of the variable that has a type and current value (e.g the type could be integer and the value perhaps is 42). But you can assign a new value to the name and the new value can have a different type as well as a different value. (e.g. the same name could now be a string whose value is "Hello, World"). In other programming languages, the variable is associated with a specific piece of memory and a specific size of memory, based on the type of the variable. An integer might be 32 bits of memory. No room there to store a longish string like "Hello, World".. But in Python, a value occupies its own memory locations of whatever size it needs as well as carrying its own type information. When you assign a new value to a name, the name is now associated with the new value's memory location, not the old hunk of memory where it's old value was stored. That might lead you to wonder what happens to the memory where the old value resided. The answer is "garbage collection" reclaims that memory for re-use automatically behind the scenes.slightly related topic that I won't really cover here is "monkey patching". A Python class can be assigned new attributes on the fly. You can even change the definitions of existing attributes at run time. That kind of thing can make it very hard to understand a program, so it is best reserved in the bag of tricks to pull out when you are debugging a complicated problem.
Another related topic to consider is aliasing where more than one name refers to the same value. This can seem quite mysterious when it happens if you haven't really understood what is going on under the hood. I talked about my Aha! moment with aliased lists in this blog article.Python is a marvelous programming language, but it sure isn't C.