Archive - Originally posted on "The Horse's Mouth" - 2015-01-28 18:14:49 - Graham Ellis
In Python, "everything is as object" and objects / variables share the same data storage area and naming convention if they're "classic" variables containing data values used in your program, or if they're names pieces of code (functions or methods). That sharing gives a great power in facilities to the language, as well as leading on occasions to some suprises.
From the course I'm running this week - an example [here].
• I've defined a function in one of two different ways within a condtional - it's just a code variable, so I can do that!
• I've assigned that code variable to a new (second) name - again, it's just an object and an assignment in Python is just the addition of an extra reference
• I've assigned something else (a string of text) to the original name, so that only the new name is left referring to the function code
• I've called the code under the new name.
For the most part, this was a teaching / learning demonstration and I would not recommend such dramatic retyping in your own code. But then the flexibiity ofered to you allows for some really good coding, for passing functions as parameters, allowing collections of functions, and so on.