python 里的 __get__, __set__ 的效果是什么?

手无护鸡之力 2017-01-13 09:47:44

class Test(object):
def __init__(self):
self.value = 0

t = Test()
t.dd = 32
print t.dd
print t.__dict__
>>>
32
{'dd': 32}


为什么书上要费劲写这样的类:

class Test(object):
def __init__(self):
self._value = {}

def __get__(self, instance, instance_type):
return self._value.get(instance, 0)

def __set__(self, instance, value):
self._value[instance] = value

t = Test()
t.dd = 32
print t.dd
print t.__dict__
>>>
32
{'_value': {}, 'dd': 32}


这两种没差别呀? 多了个_value好像根本没用啊
...全文
562 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
斯温jack 2017-01-16
  • 打赏
  • 举报
回复
简单地说使用 Another().test 如果重载调用的类(Test)的__get__ 则 会返回__get__的返回值而非test(Test实例)本身
手无护鸡之力 2017-01-16
  • 打赏
  • 举报
回复
引用 1 楼 sinat_30665603 的回复:
看下面的例子:
class Test(object):
    def __init__(self):
        self._value = {}
 
    def __get__(self, instance, instance_type):
    	print "call __get__"
        return self._value.get(instance, 0)
 
    def __set__(self, instance, value):
        self._value[instance] = value
 
class Another(object):
	test = Test()

if __name__ == "__main__":
	print Another().test
返回值: call __get__ 0 __get__基本是用来处理调用这个类的类的, 但是 这里有些问题也是要明确的。 如将上述代码改为:
class Test(object):
    def __init__(self):
        self._value = {}
 
    def __get__(self, instance, instance_type):
    	print "call __get__"
        return self._value.get(instance, 0)
 
    def __set__(self, instance, value):
        self._value[instance] = value
 
class Another(object):
	def __init__(self):
		self.test = Test()

if __name__ == "__main__":
	print Another().test
输出变为: <__main__.Test object at 0x024E64B0> 可以通过如下代码知道:
class Test(object):
    def __init__(self):
        self._value = {}
 
    def __get__(self, instance, instance_type):
    	print "call __get__"
        return self._value.get(instance, 0)
 
    def __set__(self, instance, value):
        self._value[instance] = value
 
class Another0(object):
	def __init__(self):
		self.test = Test()

class Another1(object):
	test = Test()

if __name__ == "__main__":
	print dir(Another0)
	print dir(Another1)

	print dir(Another0())
	print dir(Another1())
上述对于类的初始化方法 不能在__init__中进行 个人比较讨厌python这种“初始化”不明确“导致的可能混淆”
似懂非懂……
斯温jack 2017-01-13
  • 打赏
  • 举报
回复
看下面的例子:
class Test(object):
    def __init__(self):
        self._value = {}
 
    def __get__(self, instance, instance_type):
    	print "call __get__"
        return self._value.get(instance, 0)
 
    def __set__(self, instance, value):
        self._value[instance] = value
 
class Another(object):
	test = Test()

if __name__ == "__main__":
	print Another().test
返回值: call __get__ 0 __get__基本是用来处理调用这个类的类的, 但是 这里有些问题也是要明确的。 如将上述代码改为:
class Test(object):
    def __init__(self):
        self._value = {}
 
    def __get__(self, instance, instance_type):
    	print "call __get__"
        return self._value.get(instance, 0)
 
    def __set__(self, instance, value):
        self._value[instance] = value
 
class Another(object):
	def __init__(self):
		self.test = Test()

if __name__ == "__main__":
	print Another().test
输出变为: <__main__.Test object at 0x024E64B0> 可以通过如下代码知道:
class Test(object):
    def __init__(self):
        self._value = {}
 
    def __get__(self, instance, instance_type):
    	print "call __get__"
        return self._value.get(instance, 0)
 
    def __set__(self, instance, value):
        self._value[instance] = value
 
class Another0(object):
	def __init__(self):
		self.test = Test()

class Another1(object):
	test = Test()

if __name__ == "__main__":
	print dir(Another0)
	print dir(Another1)

	print dir(Another0())
	print dir(Another1())
上述对于类的初始化方法 不能在__init__中进行 个人比较讨厌python这种“初始化”不明确“导致的可能混淆”

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧