【发布时间】:2016-11-07 03:25:20
【问题描述】:
我正在学习 Python OOP,并谈到了装饰器这一主题,但我用于学习的材料并未深入介绍它。 我发布示例代码:
class Duck:
def __init__(self, **kwargs):
self.properties = kwargs
def quack(self):
print("Quaaack!")
def walk(self):
print("Walk like a duck.")
def get_properties(self):
return self.properties
def get_property(self, key):
return self.properties.get(key, None)
@property
def color(self):
return self.properties.get("color", None)
@color.setter
def color(self, c):
self.properties["color"] = c
@color.deleter
def color(self):
del self.properties["color"]
def main():
donald = Duck()
donald.color = "blue"
print(donald.color)
if __name__ == "__main__": main()
你能帮我理解装饰器的重要性吗? 能否用简单的语言解释一下装饰器的概念?
【问题讨论】:
-
网上有很多关于装饰器的文章。例如。 thecodeship.com/patterns/guide-to-python-function-decorators
-
Python - Decorators的可能重复
-
我只需要有人用简单的话解释一下装饰器是什么。 (不仅仅是注释代码)。我不明白为什么有人对这个问题投了反对票。我查看了一个可能重复的问题,但在这些答案中没有人通常解释什么是装饰器,所以我的问题实际上是独一无二的。
-
在此处查看第二个答案 - stackoverflow.com/questions/739654/… 我同意,虽然基本上是基本的,但这个问题没有直接重复的问题 - 至少在简单搜索中没有出现过
标签: python python-3.x decorator python-decorators