0

Why is c printed first in the output "cfim" despite f() is called first?

def f(): print('f') Class c: print('c') def __init__(self): print('i') def m(self): print('m') f() c().m()

26th Apr 2021, 9:16 AM
Gbadegesin Muhammed
Gbadegesin Muhammed - avatar
1 Respuesta
+ 2
I don't know what term (if any) used to describe such behaviour. Based on the few tests I did, I just found out that a command inside class body (i.e. print()) will be executed even though no instantiation takes place for the given class. The character 'c' will still be printed on screen even if you remove or commented the last line from the code above c().m() I suspect the line print('c') Is executed as the class definition is being processed. I think this happens because class c definition appears before the call to function f(). Hope it makes a bit of sense,
28th Apr 2021, 3:49 PM
Ipang