나는 다음과 같이 또 다른보다 일반적인 클래스 메소드를 사용하여 일부 클래스 메소드를 정의하기 위해 노력하고있어 : 그 방법 중 하나를 호출 할 때functools.partial는
class RGB(object):
def __init__(self, red, blue, green):
super(RGB, self).__init__()
self._red = red
self._blue = blue
self._green = green
def _color(self, type):
return getattr(self, type)
red = functools.partial(_color, type='_red')
blue = functools.partial(_color, type='_blue')
green = functools.partial(_color, type='_green')
을하지만 내가 얻을 :
rgb = RGB(100, 192, 240)
print rgb.red()
TypeError: _color() takes exactly 2 arguments (1 given)
rgb.red(rgb)
이 작동하기 때문에 자체는 _color
으로 전달되지 않습니다.
알 수 있습니다. 감사. – Arjor
'__init__'에서'self.red = functools.partial (RGB._color, self, 'red')'는 어떻게됩니까? Python2와도 호환됩니다. – dashesy
@dashesy : 확실하지만 각 인스턴스 (메모리 비용)에 이러한 객체를 넣고 하위 클래스가이를 대체하기가 더 어려워집니다. –