如何使用获取/设置方法? [英] How to use get/set methods?
本文介绍了如何使用获取/设置方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请指出我的代码中的错误.
Please point where a bug in my code.
class Foo:
def get(self):
return self.a
def set(self, a):
self.a = a
Foo.set(10)
Foo.get()
TypeError:set()恰好接受2个位置参数(给定1个)
如何使用__get__()
/__set__()
?
推荐答案
它们是实例方法.您必须先创建Foo
的实例:
They are instance methods. You have to create an instance of Foo
first:
f = Foo()
f.set(10)
f.get() # Returns 10
这篇关于如何使用获取/设置方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文