Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)
recategorized by

Why is the code output is different in python 2 and python 3?

class A:

    def m(self):

        print("m of A called")

class B(A):

    pass

class C(A):

    def m(self):

        print("m of C called")

class D(B,C):

    pass

x = D()

x.m()

Actual Output:

$ python  diamond1.py     //python 2 used for the code 

m of A called

$ python3 diamond1.py     //python 3 used for the code

m of C called

Can somebody tell how(the order of calling) are the methods (method m) being called and why and what is the difference in their implementation in python 2 and python 3?

1 Answer

0 votes
by (25.1k points)

This is because of the existence of old style classes and new style classes in python 2 and python 3 respectively. These two style of classes have different resolution orders. If you wish to get consistent behaviour across the versions, use : class A(object):

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...