在Python中是否有一种方法可以使用多个构造函数或多个同名的方法,这些方法的参数数量不同一个或多个参数的类型?
如果没有,那么处理这种情况的最佳方式是什么?
举个例子,我组成了一个颜色类。 这个类只应该作为讨论这个的基本例子,那里有很多不必要的和/或多余的东西。
如果我可以使用不同的对象(列表,其他颜色对象或三个整数......)调用构造函数,那将是很好的,并且构造函数会相应地处理它们。在这个基本示例中,它在某些情况下使用* args和* * kwargs,但使用类方法是我想出的唯一通用方法。什么是“最佳实践”,就像解决方案一样?
除了构造函数之外,如果我想实现_ _ add _ _方法,我怎样才能让这个方法接受所有这些:一个普通的整数(添加到所有值),三个整数(第一个加到红色值等等)或另一个颜色对象(两个红色值加在一起等)?
修改
我添加了一个替代构造函数(初始化程序,_ _ init _ _),基本上可以完成我想要的所有内容。
但我坚持第一个和工厂方法。似乎更清楚。
我还添加了一个_ _ 添加 _ _,它完成了上面提到的所有事情,但我不确定它是否是好的风格。我尝试使用迭代协议并回退到“单值模式”,而不是检查特定类型。也许还是丑陋的。
我已经看了_ _ 新 _ _,感谢您的链接。
我的第一次快速尝试失败:我从* args和* * kwargs(它是一个类,一个列表等)中过滤rgb值然后调用超类的_ _ new _ _右args(只是r,g,b)将它传递给init。对'Super(cls,self)._ _ new _ _(....)'的调用有效,但是因为我生成并返回与我调用的对象相同的对象(按预期),所有原始args传递给_ _ init _ _(按预期工作),所以它保释。
我可以摆脱_ _ init _ _ completly并设置_ _ new _ _中的值但我不知道......感觉我在这里滥用东西;-)我我应该好好看看元类和新的第一个。
来源:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
class Color (object):
# It's strict on what to accept, but I kinda like it that way.
def __init__(self, r=0, g=0, b=0):
self.r = r
self.g = g
self.b = b
# Maybe this would be a better __init__?
# The first may be more clear but this could handle way more cases...
# I like the first more though. What do you think?
#
#def __init__(self, obj):
# self.r, self.g, self.b = list(obj)[:3]
# This methods allows to use lists longer than 3 items (eg. rgba), where
# 'Color(*alist)' would bail
@classmethod
def from_List(cls, alist):
r, g, b = alist[:3]
return cls(r, g, b)
# So we could use dicts with more keys than rgb keys, where
# 'Color(**adict)' would bail
@classmethod
def from_Dict(cls, adict):
return cls(adict['r'], adict['g'], adict['b'])
# This should theoreticaly work with every object that's iterable.
# Maybe that's more intuitive duck typing than to rely on an object
# to have an as_List() methode or similar.
@classmethod
def from_Object(cls, obj):
return cls.from_List(list(obj))
def __str__(self):
return "<Color(%s, %s, %s)>" % (self.r, self.g, self.b)
def _set_rgb(self, r, g, b):
self.r = r
self.g = g
self.b = b
def _get_rgb(self):
return (self.r, self.g, self.b)
rgb = property(_get_rgb, _set_rgb)
def as_List(self):
return [self.r, self.g, self.b]
def __iter__(self):
return (c for c in (self.r, self.g, self.b))
# We could add a single value (to all colorvalues) or a list of three
# (or more) values (from any object supporting the iterator protocoll)
# one for each colorvalue
def __add__(self, obj):
r, g, b = self.r, self.g, self.b
try:
ra, ga, ba = list(obj)[:3]
except TypeError:
ra = ga = ba = obj
r += ra
g += ga
b += ba
return Color(*Color.check_rgb(r, g, b))
@staticmethod
def check_rgb(*vals):
ret = []
for c in vals:
c = int(c)
c = min(c, 255)
c = max(c, 0)
ret.append(c)
return ret
class ColorAlpha(Color):
def __init__(self, r=0, g=0, b=0, alpha=255):
Color.__init__(self, r, g, b)
self.alpha = alpha
def __str__(self):
return "<Color(%s, %s, %s, %s)>" % (self.r, self.g, self.b, self.alpha)
# ...
if __name__ == '__main__':
l = (220, 0, 70)
la = (57, 58, 61, 255)
d = {'r': 220, 'g': 0, 'b':70}
da = {'r': 57, 'g': 58, 'b':61, 'a':255}
c = Color(); print c # <Color(0, 0, 0)>
ca = ColorAlpha(*la); print ca # <Color(57, 58, 61, 255)>
print '---'
c = Color(220, 0, 70); print c # <Color(220, 0, 70)>
c = Color(*l); print c # <Color(220, 0, 70)>
#c = Color(*la); print c # -> Fail
c = Color(**d); print c # <Color(220, 0, 70)>
#c = Color(**da); print c # -> Fail
print '---'
c = Color.from_Object(c); print c # <Color(220, 0, 70)>
c = Color.from_Object(ca); print c # <Color(57, 58, 61, 255)>
c = Color.from_List(l); print c # <Color(220, 0, 70)>
c = Color.from_List(la); print c # <Color(57, 58, 61, 255)>
c = Color.from_Dict(d); print c # <Color(220, 0, 70)>
c = Color.from_Dict(da); print c # <Color(57, 58, 61, 255)>
print '---'
print 'Check =', Color.check_rgb('1', 0x29a, -23, 40)
# Check = [1, 255, 0, 40]
print '%s + %s = %s' % (c, 10, c + 10)
# <Color(57, 58, 61)> + 10 = <Color(67, 68, 71)>
print '%s + %s = %s' % (c, ca, c + ca)
# <Color(57, 58, 61)> + <Color(57, 58, 61, 255)> = <Color(114, 116, 122)>
答案 0 :(得分:10)
你可以有工厂方法,很好。但为什么不直接称之为呢?
Color(r, g, b)
Color(*[r, g, b])
Color(**{'r': r, 'g': g, 'b': b})
这是python的方式。至于from对象构造函数,我更喜欢这样的东西:
Color(*Color2.as_list())
显式优于隐式 - Python Zen
答案 1 :(得分:9)
通常,使用标记为@classmethod
的工厂方法。它们也可以在子类上正常工作。从设计的角度来看,它们更加明确,特别是当它们有一个好名字时。
在这种情况下,将所有内容混合在一起可能更方便,但它也使得构造函数的合同更加困难。
答案 2 :(得分:7)
Python不接受具有相同名称的多个方法,句点。一种方法做了一件事。
我已经看到了关于如何处理这种方法的不同方法......类方法(如上所述)或工厂功能。我最喜欢关键字参数。
class Color (object):
def __init__(self, **parms):
if parms.get('list'):
self.r, self.g, self.b = parms['list']
elif parms.get('color'):
color = parms['color']
self.r = color.r
self.g = color.g
self.b = color.b
else:
self.r = parms['red']
self.g = parms['green']
self.b = parms['blue']
c1 = Color(red=220, green=0, blue=270)
c2 = Color(list=[220, 0, 70])
c3 = Color(color=c1)
这符合Python显式和可读的方式,而且它可以轻松地允许您在需要时添加新参数。
编辑:另外,我不必查看实际的构造函数代码来理解参数。解释由关键字提供。
答案 3 :(得分:2)
在__add__
问题上:
首先,你不能得到“三个整数”,我假设你的意思是一个3元组的整数?
在这种情况下,您不会绕过某些isinstance
来电:
def __add__(self, other):
if isinstance(other, Color):
...
elif isinstance(other, (int, long)):
...
elif len(other) == 3 and all(isinstance(e, (int, long)) for e in other):
...
else:
raise TypeError("Can only add Color to Color, int or three-tuple")
您可能还想添加__radd__
的实现,以便您可以处理
1 + Color(1, 2, 3)
但那只是
def __radd__(self, other):
return self.__add__(other)
虽然严格,但在type(other) is Color
时永远不会被调用。
另外,请不要忘记__iadd__
来支持+=
。
答案 4 :(得分:1)
我的第一个建议是使用工厂方法。
虽然如果你真的想要一个方法,可以给它一些东西来发送 处理参数。
def __init__(self, method, *args, **kw):
getattr(self, '_init_'+method)(*args, **kw)
def _init_coponents(self, r, g, b):
...
def _init_fromColor(self, color):
...
并使用如下:
c1 = Color('components', 0, 0, 0,)
c2 = Color('fromColor', c1)
虽然这会增加另一个参数,但它仍然比类型测试更好,并且保持显式。它提供了非法调用的开箱即用的优秀例外,即使在子类中也可以轻松扩展。
答案 5 :(得分:0)
Python总是完全替换具有相同名称的方法。与C#不同的是,如果我没记错的话,将为不同的参数输入创建具有相同名称选项的方法。
如果关键字中只有一个变体,例如3或4个相同类型的参数,我会说使用最后一个参数的预设,或者所有这些都是可行的方法。< / p>
但是,如果你想要列表,元组和其他类型,你应该去任意参数列表并测试函数中的内容
def function(*args):
if type(args[0]) is int:
dothis()
#and so on
答案 6 :(得分:-1)
您可以检查传递给构造函数的参数类型:
def __init__(self, r = 0, g = 0, b = 0):
# if r is a list
if (type(r) == type([1,2,3])):
r, g, b = r[0], r[1], r[2]
# if r is a color
if (type(r) == type(self)):
r, g, b = r.r, r.g, r.b
self.r = r
self.g = g
self.b = b
也许这会有所帮助。