0

Why is the answer to these 2?

What is the result of this code? class A: def method(self): print(1) class B(A): def method(self): print(2) B().method()

14th Dec 2020, 7:38 AM
Masoud Amiri Batmanghlnj
Masoud Amiri Batmanghlnj - avatar
4 Answers
+ 1
it looks like class B is a subclass of class A and method overloading was used to make class B's output for the parent method 2 instead of 1.
14th Dec 2020, 7:41 AM
Slick
Slick - avatar
+ 1
Why my answer got downvote? Maybe something is wrong? Please tell me so I can correct it, maybe I got wrong in some parts. Thanks
14th Dec 2020, 8:03 AM
noteve
noteve - avatar
+ 1
2
19th Dec 2020, 10:34 PM
Kasper Riis ZĂŒlow
Kasper Riis ZĂŒlow  - avatar
- 1
Because the child class has also its "method" function or method. Hence, it will override the method of the Super or Parent class - - - -Child class without the function "method" - - - - class A: def method(self): print(1) class B(A): pass B().method() >> 1 #This code's output is 1 because the Child class don't have the function "method" thus the Parent Class' method is executed because no similar function overrides it.
14th Dec 2020, 7:41 AM
noteve
noteve - avatar