0
class A:
    print('class A')
    def __init__(self):
        print('---1')
        print('1')
class B:
    print('class B')
    def __init__(self):
        print('sss')


class C(A,B):
    print('222')
    def __init__(self):
        return 11

class D(B,A):
    print('pp')


d1 = D()

I'm getting output:

class A

class B

222

pp

sss

Why 222 is getting print without creating an object for Class C.

1

1 Answer 1

1

Remove d1 = D() and you'll see that the first three lines are still printed.

This is because unlike function definitions, class definitions are not deferred until the class is constructed. The interpreter instead executes each line as it parses it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.