我发现了这种模式(或反模式),我对此非常满意。
我觉得它很敏捷:
def example():
age = ...
name = ...
print "hello %(name)s you are %(age)s years old" % locals()
有时我会使用它的表亲:
def example2(obj):
print "The file at %(path)s has %(length)s bytes" % obj.__dict__
我不需要创建人工元组和计数参数并将 %s 匹配位置保留在元组内。
你喜欢它吗?会/会使用它吗?是/否,请解释。
最佳答案
在一百万年里都不会发生。尚不清楚格式化的上下文是什么:locals
几乎可以包括任何变量。 self.__dict__
不是那么模糊。让 future 的开发人员为什么是本地的,什么不是本地的而摸不着头脑,这真是太糟糕了。
这是一个故意的谜。为什么要让您的组织面临这样的 future 维护难题?
关于Python:使用 "..%(var)s.."% locals() 是一个好习惯吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1550479/