我有一个类,它输出大量的textcunk,这取决于一个初始参数,我希望它对文本进行右/左/中对齐。我想知道是否可以使用方法对象(http://docs.python.org/py3k/tutorial/classes.html方法-对象)而不是像
class textOutput(object):
def __init__(self):
self.justification = "left"
def justify(self,text):
if self.justification == "right":
return text.rjust(self._width)
elif self.justification == "center":
return text.center(self._width)
else:
return text.ljust(self._width)
def output(self):
for i in range(1 to 1000000):
print self.justify(i)
我想使用这样的函数对象(替换上面的justify方法)
^{2}$但是,我不知道我得到了string数据类型成员的functionobjects。在
我怎样才能实现后者呢?(前者有太多不必要的不利于绩效的检查)
注:我必须使用jython,它基本上是python2.5
p.p.s.:然后使用以下类方法进行调整切换:
你也可以
在文件的开头,或使用
^{pr2}$而不是
string
的相应用法。另外,在当前代码中不能调用setJustification
。在我希望您看到getattr方法从string中获取函数对象。 下面的代码行应该可以让你的东西正常工作
相关问题
PyPI热门下载资源包