Last active
August 29, 2015 14:05
-
-
Save period331/8746cd6eeef9b073ee12 to your computer and use it in GitHub Desktop.
meta class
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
# coding: utf-8 | |
_STANDARDS = '__standards__' | |
class _StdData(type): | |
def __new__(mcs, name, bases, attrs): | |
super_new = super(_StdData, mcs).__new__ | |
parents = [b for b in bases if isinstance(b, _StdData)] | |
if not parents: | |
return super_new(mcs, name, bases, attrs) | |
new_class = super_new(mcs, name, bases, attrs) | |
print(">>>>>>>", new_class, getattr(new_class, _STANDARDS), attrs, name, bases) | |
for attr, va in attrs.items(): | |
if attr.startswith('_'): | |
continue | |
if callable(va): | |
continue | |
print('before<><><><>', new_class, getattr(new_class, _STANDARDS)) | |
getattr(new_class, _STANDARDS)[attr] = va | |
print('after<><><><>', new_class, getattr(new_class, _STANDARDS)) | |
return new_class | |
class StdData(object): | |
pass | |
StdData = _StdData('StdData', (StdData,), {_STANDARDS: dict()}) | |
class A(StdData): | |
nameA = 'zhangming' | |
class B(StdData): | |
nameB = 'lisi' | |
if __name__ == '__main__': | |
b = B() | |
print(b), getattr(b, _STANDARDS) |
我这里是想实现在import包的时候将class中的用户定义的属性保存在class中的__standards__
属性中,供后面调用
# coding: utf-8
_STANDARDS = '__standards__'
class _StdData(type):
def __new__(mcs, name, bases, attrs):
super_new = super(_StdData, mcs).__new__
parents = [b for b in bases if isinstance(b, _StdData)]
if not parents:
return super_new(mcs, name, bases, attrs)
new_class = super_new(mcs, name, bases, attrs)
standards = {}
for attr, va in attrs.items():
if attr.startswith('_'):
continue
if callable(va):
continue
standards[attr] = va
setattr(new_class, _STANDARDS, standards)
return new_class
class StdData(object):
pass
StdData = _StdData('StdData', (StdData,), {})
class A(StdData):
nameA = 'zhangming'
class B(StdData):
nameB = 'lisi'
if __name__ == '__main__':
b = B()
print(b, getattr(b, _STANDARDS))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output: