开发者

Who can call __get__, __set__ and __del__?

This is my code. I don't know why it doesn't work.

class a:
    def __get__(self):
        return 'xxx'
    def aa(self):
        print 'aaaa'

b=a()
print b.get('aa')

Please try to answer in code, because my E开发者_运维百科nglish is not very good. Thank you.

class HideX(object):
    def __init__(self, x):
        self.x = x

    def get_x(self):
        return self.__x

    def set_x(self, x):
        self.__x = x+10

    x = property(get_x, set_x)

inst = HideX(20)
print inst.x
inst.x = 30
print inst.x


You are calling obj.get, but there is no get function in class A, hence error, either rename __get__ to get or if you by chance are trying to use descriptors do something like this

class A(object):
    def __get__(self, obj, klass):
        print "__get__", obj, klass
        return 'xxx'

class X(object):
    a = A()

x=X()
print x.a


I think you should read a bit more on Descriptors before you try to use them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜