why is 'c' printed before anything else in code sample below?

Viewed 71
def f(): 
    print("f")
class C: 
    print("c")

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

I expect that since f() is called first, the output should start with f but the output is 'cfim'

2 Answers

C is printed first because the print function execute during the creation of the class itself. Print('c') will be executed in the same way a class attribute will be set during the class definition.

A Function gets executed only when it is called. However, the class will be processed in the beginning (order-wise) and so the print statement inside the class will get executed

Related