Python: is using “..%(var)s..” % locals() a good practice?

后端 未结 7 1020
太阳男子
太阳男子 2020-11-27 13:02

I discovered this pattern (or anti-pattern) and I am very happy with it.

I feel it is very agile:

def example():
    age = ...
    name = ...
    pri         


        
7条回答
  •  广开言路
    2020-11-27 13:37

    Regarding the "cousin", instead of obj.__dict__, it looks a lot better with new string formatting:

    def example2(obj):
        print "The file at {o.path} has {o.length} bytes".format(o=obj)
    

    I use this a lot for repr methods, e.g.

    def __repr__(self):
        return "{s.time}/{s.place}/{s.warning}".format(s=self)
    

提交回复
热议问题