谁可以调用 __get__、__set__ 和 __del__?
这是我的代码。我不知道为什么它不起作用。
class a:
def __get__(self):
return 'xxx'
def aa(self):
print 'aaaa'
b=a()
print b.get('aa')
请尝试用代码回答,因为我的英语不是很好。谢谢。
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
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 English 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在调用
obj.get
,但class A
中没有 get 函数,因此出现错误,将
__get__
重命名为get
或者如果您偶然尝试使用描述符,请执行以下操作You are calling
obj.get
, but there is no get function inclass A
, hence error,either rename
__get__
toget
or if you by chance are trying to use descriptors do something like this我认为您应该在尝试使用 描述符 之前阅读更多内容。
I think you should read a bit more on Descriptors before you try to use them.