1
我想了解不同的东西来理解装饰器和Python中的函数。 是下面的代码正确的方法:python中的绑定函数和装饰器
import math
def calculate_area(func):
def area(a,b):
return a+b
return area
class Donut():
def __init__(self, outer, inner):
self.inner = inner
self.outer = outer
@calculate_area
@staticmethod
def area(self):
outer, inner = self.radius, self.inner
return Circle(outer).area() - Circle(inner).area()
将“staticmenthod”装饰讲述了内置的默认元类类型(类一类的,比照这个问题)无法创建绑定方法? 是有可能这样做:
Donut.area(4,5)
Traceback (most recent call last):
File "<pyshell#33>", line 1, in <module>
Donut.area(4,5)
TypeError: unbound method area() must be called with Donut instance as first argument (got int instance instead)
请帮我了解绑定方法与非绑定方法以及它的对他们的装饰影响。
为什么这个工程和OP的代码不会将是很好的解释。 – delnan 2012-04-08 20:29:58
刚刚编辑,请参阅编辑 – pylover 2012-04-08 21:06:28
感谢您的回复,并让我明白。我只有1个问题:_area()中的_是否意味着某些特定的问题? – 2012-04-08 21:08:41