Created
April 28, 2017 16:21
-
-
Save profhase/9d29cf0fa6cdb73e13ecf98af36bba98 to your computer and use it in GitHub Desktop.
python meta subclass
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 MyMeta(type): | |
def __new__(mcs, name, bases, clsdict): | |
clsdict['fields'] = {k: v | |
for k, v in clsdict.items() | |
if not k.startswith("_")} | |
return super(MyMeta, mcs).__new__(mcs, name, bases, clsdict) | |
class MyBaseClass(metaclass=MyMeta): | |
fields = {} | |
class SubClass(MyBaseClass): | |
param1 = 1 # meets <my_condition> | |
class SubSubClass(MyBaseClass): | |
pass | |
if __name__ == "__main__": | |
print("SubClass.fields: (expect {'param1': 1})" | |
print(SubClass.fields) | |
print("SubSubClass.fields: (expect {'param1': 1})" | |
print(SubSubClass.fields) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment