确定class属性是否为只读数据描述符

时间:2015-04-14 15:46:52

标签: python python-3.x

只读数据描述符是定义__get____set__的描述符,但__set__在调用时会引发AttributeError

一个例子是一个简单的只读属性:

class Test():

    _i = 1

    @property
    def i(self):
        return self._i

assert hasattr(Test.i, '__get__')
assert hasattr(Test.i, '__set__')
t = Test()
t.i # 1
t.i = 2 # ERROR

如果我有一个类的实例,我可以通过这种方式确定实例属性是否是只读数据描述符(虽然我根本不喜欢这样):

def is_ro_data_descriptor_from_instance(instance, attr):
    temp = getattr(instance, attr)
    try:
        setattr(instance, attr, None)
    except AttributeError:
        return True
    else:
        setattr(instance, attr, temp)
        return False

如果我知道该类不需要实例化任何参数,我可以确定它的class属性是否是与上面类似的只读数据描述符:

def is_ro_data_descriptor_from_klass(klass, attr):
    try:
        setattr(klass(), attr, None)
    except AttributeError:
        return True
    else:
        return False

但是,如果我提前不知道该类的签名,并且我尝试以这种方式实例化一个临时对象,我可能会收到错误:

class MyClass():
    i = 1
    def __init__(self, a, b, c):
        '''a, b, and c are required!'''
        pass

def is_ro_data_descriptor_from_klass(MyClass, 'i') # Error

如何确定类属性是否为只读数据描述符?

编辑:添加更多信息。

以下是我正在尝试的代码:

class StaticVarsMeta(type):
    '''A metaclass that will emulate the "static variable" behavior of
    other languages. For example: 

        class Test(metaclass = StaticVarsMeta):
            _i = 1
            @property
            def i(self):
                return self._i
        t = Test()
        assert t.i == Test.i'''
    statics = {}
    def __new__(meta, name, bases, dct):
        klass = super().__new__(meta, name, bases, dct)
        meta.statics[klass] = {}
        for key, value in dct.items():
            if "_" + key in dct:
                meta.statics[klass][key] = set()
                if hasattr(value, '__get__'):
                    meta.statics[klass][key].add('__get__')
                if hasattr(value, '__set__'):
                    try:
                        value.__set__(None, None)
                    except AttributeError:
                        continue
                    else:
                        meta.statics[klass][key].add('__set__')
        return klass
    def __getattribute__(klass, attr):
        if attr not in StaticVarsMeta.statics[klass]:
            return super().__getattribute__(attr)
        elif '__get__' not in StaticVarsMeta.statics[klass][attr]:
            return super().__getattribute__(attr)
        else:
            return getattr(klass, '_' + attr)
    def __setattr__(klass, attr, value):
        if attr not in StaticVarsMeta.statics[klass]:
            super().__setattr__(attr, value)
        elif '__set__' not in StaticVarsMeta.statics[klass][attr]:
            super().__setattr__(attr, value)
        else:
            setattr(klass, '_' + attr, value)

class Test(metaclass = StaticVarsMeta):
    _i = 1
    def get_i(self):
        return self._i
    i = property(get_i)

请注意以下事项:

type(Test.i) # int
type(Test.__dict__['i']) # property
Test().i = 2 # ERROR, as expected
Test.i = 2 # NO ERROR - should produce an error

2 个答案:

答案 0 :(得分:3)

这看起来很尴尬,但是根据我的评论,你可以在这里实现它:

class StaticVarsMeta(type):

    statics = {}

    def __new__(meta, name, bases, dct):
        cls = super().__new__(meta, name, bases, dct)
        meta.statics[cls] = {}
        for key, val in dct.items():
            if hasattr(val, '__get__') and hasattr(val, '__set__'):
                meta.statics[cls][key] = {'__get__'}
                try:
                    val.__set__(None, None)
                except AttributeError as err:
                    if "can't set attribute" in err.args:
                        continue
                meta.statics[cls][key].add('__set__')
        return cls

使用中:

>>> class ReadOnly(metaclass=StaticVarsMeta):
    @property
    def foo(self):
        return None


>>> class ReadWrite(metaclass=StaticVarsMeta):
    @property
    def bar(self):
        return None
    @bar.setter
    def bar(self, val):
        pass


>>> StaticVarsMeta.statics
{<class '__main__.ReadOnly'>: {'foo': {'__get__'}}, 
 <class '__main__.ReadWrite'>: {'bar': {'__get__', '__set__'}}}

这更像是“10 for 的首发,必须有更好的方法来实现它...

答案 1 :(得分:2)

通过尝试分配已有的值,您的第一个解决方案可以变得更简单,更健壮。这样,不需要撤消(仍然,这不是线程安全的)。

def is_ro_data_descriptor_from_instance(instance, attr):
    temp = getattr(instance, attr)
    try:
        setattr(instance, attr, temp)
    except AttributeError:
        return True
    else:
        return False