Created
September 28, 2014 18:02
-
-
Save 1st/016499a476e7a2b2d8f6 to your computer and use it in GitHub Desktop.
Inheritance in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A(object): | |
def aa(self): | |
print 'in A' | |
return self.__class__.__name__ | |
class B(A): | |
def bb(self): | |
print 'in B' | |
return super(B, self).aa() | |
def cc(self): | |
print 'in B' | |
return self.__class__.__name__ | |
>> # tests | |
>> a = A() | |
>> a.aa() | |
in A | |
'A' | |
>> b = B() | |
>> b.aa() # printed B class, not A | |
in A | |
'B' | |
>> b.bb() | |
in B | |
in A | |
'B' | |
>> b.cc() | |
in B | |
'B' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment