locals返回当前作用域 的所有局部变量的变量名:变量值组成的字典。

例子:

url_base = 'http://yann.lecun.com/exdb/mnist/'
file_names = ['train-images-idx3-ubyte.gz',
              'train-labels-idx1-ubyte.gz',
              't10k-images-idx3-ubyte.gz',
              't10k-labels-idx1-ubyte.gz']
for file_name in file_names:
  url = (url_base+file_name).format(**locals())
  print(url)

str.format(**locals())

这样的写法真的……明明还有更能人人看懂的写法。。

相关文章:

  • 2022-12-23
  • 2022-02-14
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-06-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-03-07
  • 2022-12-23
相关资源
相似解决方案