gpt4 book ai didi

python - 如何在 Python 中处理具有不同参数集(或类型)的构造函数或方法?

转载 作者:太空狗 更新时间:2023-10-29 17:13:10 25 4
gpt4 key购买 nike

在 Python 中有没有一种方法,可以让多个构造函数或多个方法具有相同的名称,它们接受的参数数量不同,或者一个或多个参数的类型

如果不是,处理这种情况的最佳方法是什么?

举个例子,我编了一个颜色类。 这门课应该只是作为一个基本的例子来讨论这个问题,里面有很多不必要和/或多余的东西。

如果我可以用不同的对象(一个列表、另一个颜色对象或三个整数......)调用构造函数并且构造函数相应地处理它们,那就太好了。在这个基本示例中,它在某些情况下适用于 * args 和 * * kwargs,但使用类方法是我想出的唯一通用方法。什么是“最佳实践”之类的解决方案?

撇开构造函数不谈,如果我也想实现一个 _ add _ _ 方法,我怎样才能让这个方法接受所有这些:一个普通整数(添加到所有值)、三个整数(其中第一个与红色值相加等)或另一个颜色对象(其中两个红色值相加等)?

编辑

  • 我添加了一个替代构造函数(初始化器,_ _ init _ _),它基本上可以完成我想要的所有工作。

  • 但我坚持使用第一个方法和工厂方法。看起来更清楚了。

  • 我还添加了 _ _ add _ _,它完成了上述所有操作,但我不确定它是否是好的样式。我尝试使用迭代协议(protocol)并退回到“单值模式”而不是检查特定类型。也许仍然很丑。

  • 我看过_ _ _ _,感谢您提供链接。

  • 我的第一次快速尝试失败了:我从 * args 和 * * kwargs(它是一个类、一个列表等)中过滤 rgb 值,然后调用父类(super class)的 _ _ new _ _正确的参数(只是 r、g、b)将其传递给 init。对“Super(cls, self)._ _ new _ _ (....)”的调用有效,但由于我生成并返回与我调用的对象相同的对象(按预期),所有原始参数被传递给 _ _ init _ _(按预期工作),所以它保释了。

  • 我可以完全摆脱 _ _ init _ _ 并在 _ _ 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)>

最佳答案

你可以有工厂方法,这很好。但为什么不按原样调用它呢?

Color(r, g, b)
Color(*[r, g, b])
Color(**{'r': r, 'g': g, 'b': b})

这是 python 的方式。至于 from object 构造函数,我更喜欢这样的东西:

Color(*Color2.as_list())

显式优于隐式 - Python Zen

关于python - 如何在 Python 中处理具有不同参数集(或类型)的构造函数或方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/356718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com