Skip to content

Instantly share code, notes, and snippets.

@bwangelme
Created November 20, 2018 10:47
Show Gist options
  • Save bwangelme/21974724acdcdb9e1a4a917faf9b424c to your computer and use it in GitHub Desktop.
Save bwangelme/21974724acdcdb9e1a4a917faf9b424c to your computer and use it in GitHub Desktop.
Python 继承的时候会丢掉 `__x` 命名的属性
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