Using locals() and format() method for strings: are there any caveats?

前端 未结 3 1098
傲寒
傲寒 2020-12-29 01:04

Are there any disadvantages, caveats or bad practice warnings about using the following pattern?

def buildString(user, name = \'john\', age=22):
    userId =         


        
3条回答
  •  -上瘾入骨i
    2020-12-29 01:43

    Pre Python 3.6 answer

    This is very old, but if you find yourself using .format the one caveat I have encountered with passing in **locals is that if you don't have that variable defined anywhere, it will break. Explicitly stating what variables are passed in will avoid this in most modern IDEs.

    foo = "bar"
    "{foo} and {baz} are pair programming".format(**locals())
    
    

提交回复
热议问题