recipe-525492.py 文件源码

python
阅读 31 收藏 0 点赞 0 评论 0

项目:code 作者: ActiveState 项目源码 文件源码
def __new__(cls, name, bases, dct):
    slots = dct.get('__slots__', [])
    orig_slots = []
    for base in bases:
        if hasattr(base, "__slots__"):
        orig_slots += base.__slots__
    if '__init__' in dct:
        init = dct['__init__']
        initproc = type.__new__(cls, name, bases, dct)
        initproc_source = inspect.getsource(initproc)
        ast = compile(initproc_source, "dont_care", 'exec', _ast.PyCF_ONLY_AST)
        classdef = ast.body[0]
        stmts = classdef.body
        for declaration in stmts:
        if isinstance(declaration, _ast.FunctionDef):
            name1 = declaration.name
            if name1 == '__init__': # delete this line if you do not initialize all instance variables in __init__
            initbody = declaration.body
            for statement in initbody:
                if isinstance(statement, _ast.Assign):
                for target in statement.targets:
                    name1 = target.attr
                    if name1 not in orig_slots:
                    slots.append(name1)
        if slots:
        dct['__slots__'] = slots
    return type.__new__(cls, name, bases, dct)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号