请看下面代码:
class CLanguage:
def init(self):
self.b1 = “b21”
def aaa(self, c1):
self.a1 = c1
self.a3 = “shazi”
print(“shazi3”)
def ddd(self, c2):
self.a2 = “nihao”
def getname(self):
return “hello”
b1 = property(getname, aaa)
clang = CLanguage()
#print(clang.b1)
#print(clang.a3)
print(clang.a1)
#clang.a1=“Python教程”
#print(clang.a1)
输出:
shazi3
b21
稍作改动:
class CLanguage:
def init(self):
self.b1 = “b21”
def aaa(self, c1):
self.a1 = “c1”
self.a3 = “shazi”
print(“shazi3”)
def ddd(self, c2):
self.a2 = “nihao”
def getname(self):
return “hello”
b1 = property(getname, aaa)
clang = CLanguage()
#print(clang.b1)
#print(clang.a3)
print(clang.a1)
#clang.a1=“Python教程”
#print(clang.a1)
输出:
shazi3
c1
目前来看只要对b1赋值,就会调用property的第二个方法(创建对象时__init__方法对b1进行了赋值)
只要对b1读操作就会调用property的第一个方法