开发者

How to use descriptors with attributes of instances?

I read the docs and sa开发者_运维技巧w that descriptors had to be used with class attributes only. What would be an acceptable way of using them within instances of that class?

I.e.

class Attribute( object ):
    def __init__(self, value):
        self.value = value

    def __get__(self, obj, objtype):
        print "GETTING"
    def __set__(self,obj,val):
        print "SETTING"

class MyClass( object ):
    def __init__(self):
        self.myname = Attribute( '' )

name = MyClass()
print name.myname


Manipulate them via obj, the second argument to the descriptor's methods.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜