Python 中,通过使用描述符,可以让程序员在引用一个对象属性时自定义要完成的工作。
但是我下面例子没有描述符也能访问对象属性。
class Bird:
str1 = “hello”
#鸟有翅膀
def isWing(self):
print(“鸟有翅膀”)
#鸟会飞
def fly(self):
print(“鸟会飞”)
class Ostrich():
# 重写Bird类的fly()方法
def fly(self):
print(“鸵鸟不会飞”)
x = Bird()
#创建Ostrich对象
ostrich = Ostrich()
ostrich.x.fly()
print(ostrich.x.str1)、
输出:
鸟会飞
hello
Python描述符貌似没啥用
最新推荐文章于 2024-07-27 12:20:46 发布