Created
May 20, 2013 02:34
-
-
Save wyukawa/5610137 to your computer and use it in GitHub Desktop.
python inherit instance variable
This file contains hidden or 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 __init__(self): | |
| self.a = 1 | |
| def hoge(self): | |
| if hasattr(self, "a"): | |
| return self.a | |
| else: | |
| return 0 | |
| class B(A): | |
| def __init__(self): | |
| # self.a = 1 | |
| self.b = 2 | |
| if __name__ == '__main__': | |
| b = B() | |
| print b.hoge() # print 0. not 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment