'for _ in range()'是什么意思? [英] what does 'for _ in range()' mean?

查看:525
本文介绍了'for _ in range()'是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了这段代码:


sock.listen(20)

for _ in range(20):

newsock,client_addr = sock.accept()

print" Client connected:",client_addr

data [newsock] =""


为什么在这个例子中使用_?是否有任何

优化使用它?


我知道在翻译中_表示

的最后一个值计算,但_是什么意思

源代码里面?

I saw this code snippet:

sock.listen(20)
for _ in range(20):
newsock, client_addr = sock.accept()
print "Client connected:", client_addr
data[newsock] = ""

why use _ for this example? Is there any
optimization to be had using it?

I know that in the interpreter _ means the
last value calculated, but what does _ mean
inside source code?

推荐答案

文章< 2m ****** ******@uni-berlin.de> ;,

Jon Perez< jb ******** @ wahoo.com>写道:
In article <2m************@uni-berlin.de>,
Jon Perez <jb********@wahoo.com> wrote:
我看到了这个代码片段:

sock.listen(20)
for _ in range(20):
newsock,client_addr = sock.accept()
打印"客户连接:",client_addr
数据[newsock] =""

为什么使用_作为这个例子?是否有使用它的优化?

我知道在解释器中_表示最后计算的值,但_是什么意思
内部来源代码?
I saw this code snippet:

sock.listen(20)
for _ in range(20):
newsock, client_addr = sock.accept()
print "Client connected:", client_addr
data[newsock] = ""

why use _ for this example? Is there any
optimization to be had using it?

I know that in the interpreter _ means the
last value calculated, but what does _ mean
inside source code?




AFAIK它只是一个变量,与其他任何变量一样,但按照惯例,它意味着你不打算使用
使用该值,只需阅读并忽略它。


-

David Eppstein

计算机科学系,大学。加利福尼亚州,Irvine
http://www.ics.uci。 edu /~eppstein /


文章< ep ********************* *******@news.service.uci.e du>,

David Eppstein< ep ****** @ ics.uci.edu>写道:
In article <ep****************************@news.service.uci.e du>,
David Eppstein <ep******@ics.uci.edu> wrote:
在文章< 2m ************ @ uni-berlin.de>,
Jon Perez< jb ** ******@wahoo.com>写道:
In article <2m************@uni-berlin.de>,
Jon Perez <jb********@wahoo.com> wrote:
我看到了这个代码片段:

sock.listen(20)
for _ in range(20):
newsock,client_addr = sock.accept()
打印"客户连接:",client_addr
数据[newsock] =""

为什么使用_作为这个例子?是否有使用它的优化?

我知道在解释器中_表示最后计算的值,但_是什么意思
内部来源代码?
I saw this code snippet:

sock.listen(20)
for _ in range(20):
newsock, client_addr = sock.accept()
print "Client connected:", client_addr
data[newsock] = ""

why use _ for this example? Is there any
optimization to be had using it?

I know that in the interpreter _ means the
last value calculated, but what does _ mean
inside source code?



AFAIK它只是一个变量,与其他任何变量一样,但按照惯例,它意味着你不打算使用这个值,只需阅读它并忽略它。



AFAIK it''s just a variable like any other, but by convention it means
that you don''t intend to use that value, just read it and ignore it.




我之前从未听说过这个惯例。它是一些特定于python的

的东西,还是我的无知在范围上更加全球化?


无论如何,我会投票支持更实用的东西变量名。在上面的

案例中,类似于connectionNumber或其他什么会更多

自我解释。



I''ve never heard of that convention before. Is it some python-specific
thing, or is my ignorance more global in scope?

In any case, I''d vote for some more useful variable name. In the above
case, something like connectionNumber or whatever would be more
self-explanitory.


Roy Smith < ro*@panix.com>写道:
Roy Smith <ro*@panix.com> writes:
我以前从未听说过这个约定。它是一些特定于python的东西,还是我的无知在范围上更全局?
I''ve never heard of that convention before. Is it some python-specific
thing, or is my ignorance more global in scope?




它可能来自Prolog,你使用_作为一个未绑定的通配符。

例如如果X是父亲或母亲,则宣布X是父母:


父母(X): - fatherOf(X,_),.。

父母(X): - motherOf(X,_),!。


因为你不需要知道关于这个孩子的任何事情,只需它就可以了。

存在。如果你使用像Y这样的变量,你或者还需要

说一些关于Y的东西,否则Y会在解决的答案中结束。


( !是一个剪切,意味着我们对多个答案不感兴趣。



It may derive from Prolog, where you use _ as an unbound "wildcard".
E.g. to declare that X is a parent if X is a father or mother:

parent(X) :- fatherOf(X, _), !.
parent(X) :- motherOf(X, _), !.

because you don''t need to know anything about the child, just that it
exists. If you used a variable like Y, you would either also need to
say something about Y, or Y would end up in the resolved answer.

(The ! is a "cut", and means we''re not interested in more than one
answer.)


这篇关于'for _ in range()'是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆