functools.partial上课方法 [英] functools.partial on class method
问题描述
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)
打印rgb.red()
TypeError:_color()只需要2个参数(1个给定)
我猜自己不会传递给 _color
,因为 rgb.red(rgb)
工作。
解决方案在功能上分配部分,而不是方法。 functools.partial()
对象不是描述符,它们本身不会添加 self
参数,不能作为方法本身。您只能 包装绑定的方法或函数,它们根本无法使用未绑定的方法。这是记录的:
对象就像函数
对象,因为它们是可调用的,弱可引用的,和可以有属性。有一些重要的区别。例如,不会自动创建 __ name __
和 __ doc __
属性。另外,在类中定义的 partial
对象的行为类似于静态方法,在实例属性查询期间不转换为绑定的方法。
使用属性
s;
class RGB(object):
def __init __(self,红色,蓝色,绿色):
超级(RGB,自身).__初始__()
self._red =红色
self._blue =蓝色
self._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')
从Python 3.4开始,您可以使用新的 functools.partialmethod()
object here;它会在绑定到一个实例时做正确的事情:
class RGB(object):
def __init __自我,红色,蓝色,绿色):
超级(RGB,自我).__初始__()
self._red =红色
self._blue =蓝色
self._绿色=绿色
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')
但这些必须被调用,而属性
对象可以用作简单的属性。 >
I'm trying to define some class methods using another more generic class method as follows:
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')
But when i attempt to invoke any of those methods i get:
rgb = RGB(100, 192, 240)
print rgb.red()
TypeError: _color() takes exactly 2 arguments (1 given)
I guess self is not passed to _color
since rgb.red(rgb)
works.
解决方案 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.
这篇关于functools.partial上课方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!