python 之中使用单下划线“_”做变量的目的?

2016年11月09日 10891点热度 1人点赞 0条评论

在阅读Google的免费深度学习课程练习代码的时候,看到下面一段代码:

注意第17行,第一个变量居然是一个下划线!

在查阅了一些资料之后,有人是这么描述单个下划线的使用场景:

  1. 在交互式的时候,保持上一次执行的结果
    1. 没看懂。原文如下:
    2. To hold the result of the last executed statement in an interactive interpreter session. This precedent was set by the standard CPython interpreter, and other interpreters have followed suit
  2. i18n的时候,自动加载翻译
  3. 可以直接忽略、丢弃的变量

 

我们再回原始代码看看:https://github.com/tensorflow/tensorflow/blob/3737ac321e67410bf061257d5f644eae8abbf79b/tensorflow/examples/udacity/2_fullyconnected.ipynb

Ctrl+F 搜索一下,可以看到下面的代码完全没使用这个变量。 因此在这里的作用,应该属于第三种: 你完全可以忽略。。。

 

今天又学习了一个小技巧...

本文为原创文章,转载请注明出处
原文链接:http://www.flyml.net/2016/11/09/purpose-of-the-single-underscore-variable-in-python/

 

 

RangerWolf

保持饥渴的专注,追求最佳的品质

文章评论