、私は次のように他のより一般的なクラスメソッドを使用して、いくつかのクラスメソッドを定義しようとしている:私はそれらのいずれかの方法を起動しようとすると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:確かですが、それは各インスタンスにそれらのオブジェクトを置きます(メモリコスト)。また、サブクラスがそれらを置き換えるのが難しくなります。 –