You are creating partials on the function, not the method. functools.partial()
objects are not descriptors, they will not themselves add the self
argument and cannot act as methods themselves. You can only wrap bound methods or functions, they don't work at all with unbound methods. This is documented:
partial
objects are like function
objects in that they are callable, weak referencable, and can have attributes. There are some important differences. For instance, the __name__
and __doc__
attributes are not created automatically. Also, partial
objects defined in classes behave like static methods and do not transform into bound methods during instance attribute look-up.
Use property
s instead; these are descriptors:
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)
@property
def red(self): return self._color('_red')
@property
def blue(self): return self._color('_blue')
@property
def green(self): return self._color('_green')
As of Python 3.4, you can use the new functools.partialmethod()
object here; it'll do the right thing when bound to an instance:
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.partialmethod(_color, type='_red')
blue = functools.partialmethod(_color, type='_blue')
green = functools.partialmethod(_color, type='_green')
but these'd have to be called, whilst the property
objects can be used as simple attributes.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…