All this is pretty straight forward. When the module is loaded, the code runs from top to bottom. Thus, global variables will be created as they appear in the module code. When a function is called, the code in it is executed from top to bottom.
Decorators are a bit of an exception to the fact that they are called after the function they decorate has been defined in order from top to bottom. Therefore, if you have:
@hits @dies @rofls def watcher(): pass
it is the same as:
def watcher(): pass watcher = hits(dies(rofls(watcher)))
That makes sense if you think every decorator wraps everything underneath.
When an object is created, __new__ is first called, then __init__ . super() is called if you have a call to it somewhere, like everything else ... "constructor variables" are simply initialized whenever you have a variable assignment in __init__ .
EDIT: look at your example:
class A(superA): class_var = 1234 def __init__(self): superA.__init__(self) another_var = 5678 @random_decorator def method(self):
Instead, I will teach you how to fish.
def random_decorator(f): print "random_decorator called" return f def init_var(varname, val): print '%s being initialized' % (varname,) return val class superA(object): def __new__(cls): print 'superA.__new__ called' return super(superA, cls).__new__(cls) def __init__(self): print "superA.__init__ called" class A(superA): class_var = init_var('class_var', 1234) def __new__(cls): print 'A.__new__ called' return super(A, cls).__new__(cls) def __init__(self): print 'A.__init__ called' superA.__init__(self) another_var = init_var('another_var', 5678) @random_decorator def method(self): print 'method called'
See in which order the print statements appear. A.