快速创建一个只读的自定义对象
邋遢大王
posted @ 2010年9月20日 19:21
in Python
, 1029 阅读
def ROO( objectName ='ReadOnlyObject', **args): """ ------------------------------------------------ . 参数: . object名称(string, 默认值:ReadOnlyObject) . **args { 属性名: 值 } *n ------------------------------------------------ . 返回值: . Object (ReadOnly) ------------------------------------------------ . Example: . >>> obj = ROO( str1 ='my', str2 ='love') . >>> print obj . <__main__.ReadOnlyObject object at 0x00C89FD0> . >>> print obj.str1, obj.str2 . my love """ dictBI = {} args_n = [] for name, val in args.items(): dictBI[name] = val args_n.append(name) dictBI['__slots__'] = args_n return type( objectName, (object, ), dictBI)()