Home  >  Article  >  Backend Development  >  python创建只读属性对象的方法(ReadOnlyObject)

python创建只读属性对象的方法(ReadOnlyObject)

WBOY
WBOYOriginal
2016-06-06 11:28:091707browse

代码如下:


def ReadOnlyObject(**args):
    dictBI = {}
    args_n = []
    for name, val in args.items():
        dictBI[name] = val
        args_n.append(name)
    dictBI['__slots__'] = args_n
    return type('ReadOnlyObject', (object,), dictBI)()

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn