Immutable attribute in python class and type check
我正在寻找一种在Python中构建类的方法:
- setter在赋值前检查值的类型
- 无法添加新的类属性
就目前而言,我发现了这两个装饰师:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | def getter_setter_gen(name, type_): def getter(self): return getattr(self,"__" + name) def setter(self, value): print"setter", value if not isinstance(value, type_): raise TypeError("%s attribute must be set to an instance of %s" % (name, type_)) setattr(self,"__" + name, value) return property(getter, setter) def auto_attr_check(cls): new_dct = {} print"auto_attr_check", cls.__dict__.items() for key, value in cls.__dict__.items(): if isinstance(value, type): value = getter_setter_gen(key, value) new_dct[key] = value # Creates a new class, using the modified dictionary as the class dict: n = type(cls)(cls.__name__, cls.__bases__, new_dct) return n |
和
1 2 3 4 5 6 7 8 9 10 11 12 13 | def froze_it(cls): def frozensetattr(self, key, value): print key key = ''+key print(dir(self)) print key if not hasattr(self, key): raise TypeError("Class {} is frozen. Cannot set {} = {}" .format(cls.__name__, key, value)) else: object.__setattr__(self, key, value) cls.__setattr__ = frozensetattr return cls |
但是我很难加入这两种方法。你能帮助我吗?你有想法吗?谢谢
您遇到的问题是,您的
我建议两个补充的解决方案。首先,更改
1 2 3 4 5 6 | def frozensetattr(self, key, value): if not hasattr(self, key) and type(cls.__dict__.get(key)) is not property: raise TypeError("Class {} is frozen. Cannot set {} = {}" .format(cls.__name__, key, value)) else: object.__setattr__(self, key, value) |
第二个修复是对
1 2 3 4 5 | def setter(self, value): print"setter", value if not isinstance(value, type_): raise TypeError("%s attribute must be set to an instance of %s" % (name, type_)) object.__setattr__(self,"__" + name, value) |
最后一个注意事项:您的
在运行时,代码的行为与被损坏的名称直接写入源代码的行为完全相同。这意味着
如果您只想将名称非正式地标记为private(也就是说,它们不是公共API的一部分),那么您可能应该使用一个前导下划线。