>>39
Thank you for sharing a well-known information about module loading in statically typed languages.
Python is not statically typed and its modules are executed when loaded. Note that doesn't forbit modules being compiled in a sense, its just that the compiled (to bytecode but that doesn't matter) module code is executed to populate the actual module object with methods, classes and fields.
It's not limited to modules, of course. In the following example (simplified, the real thing should use __slots__ and **kwargs) the inner class definition is executed each time the function is called, yielding different class objects. Sure, it's slow as fuck and dangerous in the wrong hands, yet allows a lot of interesting techniques.
def record_class(*fieldnames):
class TempClass(object):
names = fieldnames
def __init__(self, *args):
for (name, arg) in zip(self.names, args):
setattr(self, name, arg)
return TempClass
Record = record_class("a", "b")
obj = Record(1, 2)
print obj.a, obj.b