Python 将类调用参数存储为属性
我有一个抽象类,当我将其子类化时,我希望传递的参数存储为属性,以供内部使用。 我的类定义如下:Python 将类调用参数存储为属性,python,class,python-3.x,Python,Class,Python 3.x,我有一个抽象类,当我将其子类化时,我希望传递的参数存储为属性,以供内部使用。 我的类定义如下: class Parent(object): def __new__(cls, a, b, c, d, e): obj = super(Parent, cls).__new__(cls) a, b, c, d, e = *(float(_) for _ in (a, b, c, d)), int(e) keys = ("_a", "_b", "_c
class Parent(object):
def __new__(cls, a, b, c, d, e):
obj = super(Parent, cls).__new__(cls)
a, b, c, d, e = *(float(_) for _ in (a, b, c, d)), int(e)
keys = ("_a", "_b", "_c", "_d", "_e")
obj.__dict__.update({k: v for k, v in zip(keys, (a, b, c, d, e))})
return obj
@property
def a(self):
return self._a #same for b, c, d, e
有什么共同的模式或更好的方法来做到这一点吗