【发布时间】:2013-06-14 17:31:38
【问题描述】:
我的许多类看起来像下面的类来表示帐户
class Account(object):
def __init__(self, first, last, age, id, balance):
self.first = first
self.last = last
self.age = age
self.id = id
self.balance = balance
def _info(self):
return self.first, self.last, self.age, self.id, self.balance
def __eq__(self, other):
return self._info == other._info()
def __hash__(self):
return hash((type(self), self.info()))
def ... # other methods follow
但真正唯一相关的信息是我关心的属性列表first, last, age, id, balance。是否有标准方法来定义遵循此结构的 Python 类?
乍一看,我想到了namedtuple,但我不确定这是否允许我在事后添加其他方法。真的,我想要类似下面的东西
class Account(object):
attributes = "first last age id balance"
def ... # other methods
获得这个的最好方法是什么?
【问题讨论】:
标签: python oop metaprogramming