Created
November 20, 2018 10:47
-
-
Save bwangelme/21974724acdcdb9e1a4a917faf9b424c to your computer and use it in GitHub Desktop.
Python 继承的时候会丢掉 `__x` 命名的属性
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 SuperClass(object): | |
__table_name__ = 'subject_collection' | |
__table = 'd' | |
@classmethod | |
def get(cls): | |
inst = cls() | |
inst.__class__ = SubClass | |
return inst | |
def subject_ids(self): | |
raise NotImplemented | |
class SubClass(SuperClass): | |
def subject_ids(self): | |
return [] | |
s = SuperClass.get() | |
s.subject_ids() | |
print(s.__table_name__) | |
print(s.__table) # AttributeError |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment