码迷,mamicode.com
首页 > 编程语言 > 详细

Python descriptor

时间:2015-05-19 10:08:17      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

class A:
    def __init__(self, name):
        self.name = name

    def __get__(self, ins, cls):
        print(‘call get‘)
        if ins is None:
            return self
        return ins.__dict__[self.name]

    def __set__(self, ins, val):
        print(‘call set‘)
        ins.__dict__[self.name] = val

    def __delete__(self, ins):
        print(‘cal del‘)
        del ins.__dict__[self.name]

class B:
    a1 = A(‘a1‘)

    def __init__(self):
        self.a1 = 3

#b=B.__new__(B)
b=B()
b.a1
#B.a1
    

  

Python descriptor

标签:

原文地址:http://www.cnblogs.com/xiangnan/p/4513565.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!