def __new__(cls, cls_name, cls_parents, cls_dict):
ret = super(NoninstantiableMeta,cls).__new__(cls, cls_name, cls_parents, cls_dict)
for name in dir(ret):
value = getattr(ret, name)
if isinstance(value, MethodType):
if not getattr(ret, '_allow_self_as_first_arg', False) and 'self' in inspect.getargs(value.func_code).args[:1]:
warn_message = SELF_WARN_MESSAGE % {'cls': cls_name, 'method': name}
warnings.warn(warn_message, SyntaxWarning)
new_method = classmethod(value.im_func)
setattr(ret, name, new_method) # turn all methods to classmethods
ret.__init__ = classmethod(cls._prevent_instantiating) # You shall not pass!
return ret
评论列表
文章目录