我有以下类(class):
class Enum(RootFragment):
def __init__(self, column, packageName, name, modifiers=["public"], enumValues=[]):
RootFragment.__init__(self, packageName, name, modifiers, "enum")
self.column = column
self.enumValues = []
map(self.addEnumValue, enumValues)
... more methods
现在我创建了许多放入字典的枚举实例。这是打印在 print collidingEnums
上的内容:
{'ManagerRole': <Enum instance at 0x0998F080>, 'StaffMemberRole': <Enum instance at 0x0998B490>}
现在因为 <Enum instance at 0x0998F080>
不是很有用我想调用getName
每个实例上的方法。我在尝试:
print ", ".join(map(Enum.getName, collidingEnums.items())),
但这给了我一个错误提示:
TypeError: unbound method getName() must be called with Enum instance as first argument (got tuple instance instead)
咦?你怎么称呼getName
方法在这里?有可能吗?
最佳答案
不要使用 map
。改用生成器表达式:
print ", ".join(i.getName() for i in collidingEnums.values())
或者,您可以简单地为您的 Enum
类定义您自己的 __repr__
方法,如果您只是尝试打印类。
class Enum(RootFrament):
# ...
def __repr__(self):
return self.getName()
关于python - 如何调用实例列表中的特定方法(使用 map )?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9916175/