【发布时间】:2011-10-06 06:03:06
【问题描述】:
我想做这样的事情:
class Foo(object):
def __init__(self, name):
self._name = name
self._count = 0
def getName(self):
return self._name
name = property(getName)
def getCount(self):
c = self._count
self._count += 1
return c
count = property(getCount)
def __repr__(self):
return "Foo %(name)s count=%(count)d" % self.__dict__
但这不起作用,因为name 和count 是带有getter 的属性。
有没有办法解决这个问题,以便我可以使用带有命名参数的格式字符串来调用 getter?
【问题讨论】:
标签: python class dictionary format properties