Python 的描述符“__get__”没有像我预期的那样被调用,原因是什么?
Python's descriptor "__get__" was not called as I expected, reason?
我正在尝试 python 的 get 描述符,看看它是否被调用。
我有以下内容:
"""This is the help document"""
class c1(object):
"""This is my __doc__"""
def __get__(s,inst,owner):
print "__get__"
def __init__(s):
print "__init__"
s.name='abc'
class d(object):
def __init__(s):
s.c=c1()
d1=d()
d1.c
print d1.c.name
我预计它会调用 get 函数。但事实上输出是
__init__
abc
为什么 "d1" 的实例所有者没有调用我的“get”函数?
谢谢!
描述符必须绑定到 class,而不是实例。
class d(object):
c = c1()
我正在尝试 python 的 get 描述符,看看它是否被调用。
我有以下内容:
"""This is the help document"""
class c1(object):
"""This is my __doc__"""
def __get__(s,inst,owner):
print "__get__"
def __init__(s):
print "__init__"
s.name='abc'
class d(object):
def __init__(s):
s.c=c1()
d1=d()
d1.c
print d1.c.name
我预计它会调用 get 函数。但事实上输出是
__init__
abc
为什么 "d1" 的实例所有者没有调用我的“get”函数?
谢谢!
描述符必须绑定到 class,而不是实例。
class d(object):
c = c1()