还有其他人使用这个小成语吗?
Ruby在编写循环时有一个简洁的方便,你不需要关注循环索引:你只需要做几次{...一些
code ...}其中n是一个整数,表示你想要执行多少次
来执行某些代码。
在Python中,这是一个for循环的直接翻译。当
指数对我来说无关紧要时,我倾向于把它写成:
$ x $ b for _ in xrange(1,n):
一些代码
另一种表示你不关心循环的方法
索引将是
for xrange(1,n):
一些代码
但我喜欢使用_因为它是'只有1个字符且沟通良好
这个想法我不在乎这个变量。
我能看到的唯一潜在缺点是三倍:
1.对于不熟悉它的人来说可能有些不和谐。我承认
起初看起来很奇怪。
2.变量_在交互式翻译中有特殊含义
提示。由于这个原因,可能会有一些混乱。
5.五个是正确的。 (ob圣杯参考,当然。:-)
所以,我想我想知道是否有其他人使用类似的成语,如果
它有任何缺点,我不知道。
谢谢
Paul
Ruby has a neat little convenience when writing loops where you don''t
care about the loop index: you just do n.times do { ... some
code ... } where n is an integer representing how many times you want
to execute "some code."
In Python, the direct translation of this is a for loop. When the
index doesn''t matter to me, I tend to write it as:
for _ in xrange (1,n):
some code
An alternative way of indicating that you don''t care about the loop
index would be
for dummy in xrange (1,n):
some code
But I like using _ because it''s only 1 character and communicates well
the idea "I don''t care about this variable."
The only potential disadvantages I can see are threefold:
1. It might be a little jarring to people not used to it. I do admit
it looks pretty strange at first.
2. The variable _ has special meaning at the interactive interpreter
prompt. There may be some confusion because of this.
5. Five is right out. (ob Holy Grail reference, of course. :-)
So, I guess I''m wondering if anyone else uses a similar idiom and if
there are any downsides to it that I''m not aware of.
Thanks
Paul
文章
< e8 ************************* ********* @ q39g2000hsf。 googlegroups.com>,
mi***********@gmail.com 写道:
In article
<e8**********************************@q39g2000hsf. googlegroups.com>,
mi***********@gmail.com wrote:
Ruby在编写循环时有一个简洁的方便,你不需要关注循环索引:你只做n次... {...一些
代码...}其中n是一个整数,表示你想要执行多少次
执行某些代码。
在Python中,这是一个for循环的直接翻译。当
指数对我来说无关紧要时,我倾向于把它写成:
$ x $ b for _ in xrange(1,n):
一些代码
另一种表示你不关心循环的方法
索引将是
for xrange(1,n):
一些代码
但我喜欢使用_因为它是'只有1个字符且沟通良好
这个想法我不在乎这个变量。
Ruby has a neat little convenience when writing loops where you don''t
care about the loop index: you just do n.times do { ... some
code ... } where n is an integer representing how many times you want
to execute "some code."
In Python, the direct translation of this is a for loop. When the
index doesn''t matter to me, I tend to write it as:
for _ in xrange (1,n):
some code
An alternative way of indicating that you don''t care about the loop
index would be
for dummy in xrange (1,n):
some code
But I like using _ because it''s only 1 character and communicates well
the idea "I don''t care about this variable."
不是我。如果我读for _ in ...,我就不太确定_是什么。
这是我忘记的一些神奇的语法吗?或者新的
添加到语言中,而我没有注意(我仍然认为最多
的东西从1.5添加为新的:-))。如果我看到虚拟,我知道它
表示语言需要一个变量,但不需要该值。
Not to me. If I read "for _ in ...", I wouldn''t be quite sure what _ was.
Is it some magic piece of syntax I''ve forgotten about? Or something new
added to language while I wasn''t paying attention (I still consider most
stuff added since 1.5 to be new-fangled :-)). If I see "dummy", I know it
means, "the language requires a variable here, but the value is not needed".
1.对于不熟悉它的人来说,这可能有点刺耳。我承认
起初看起来很奇怪。
2.变量_在交互式翻译中有特殊含义
提示。由于这个原因,可能会有一些混乱。
1. It might be a little jarring to people not used to it. I do admit
it looks pretty strange at first.
2. The variable _ has special meaning at the interactive interpreter
prompt. There may be some confusion because of this.
哇,我甚至都不知道#2。现在你看到我的意思了一些
魔法语法?当然,在它之间,它看起来很奇怪和可能有一些
混乱,这是不足以使用它的原因吗?
但是,更重要的是,我会尝试找到变量名称,它描述了为什么我的b $ b循环,即使我实际上并没有使用循环体中的值:
for number_that_you_shall_count_to in xrange(3):
print" Whaaaaaaa"
Wow, I didn''t even know about #2. Now you see what I mean about "some
magic syntax"? Surely, between, "It looks strange", and "there may be some
confusion", that''s enough reason not to use it?
But, more to the point, I''d try to find variable name which described why I
was looping, even if I didn''t actually use the value in the loop body:
for number_that_you_shall_count_to in xrange(3):
print "Whaaaaaaa"
En Sun, 2008年2月3日01:03:43 -0200,James Matthews< ny ******* @ gmail.com>
$ b $bescribió:
很抱歉被挑剔,但来自其他语言的人可能会因为错误的例子而感到困惑:
En Sun, 03 Feb 2008 01:03:43 -0200, James Matthews <ny*******@gmail.com>
escribió:
Sorry to be nitpicking, but people coming from other languages may get
confused by the wrong examples:
我所做的是一个简单的范围呼叫。对于我的范围(我想要的次数
重复一些事情)
我猜它来自我的C天(i = 0; i
范围内(99):
What i do is a simple range call. for i in range(number of times i want
to repeat something)
I guess it comes from my C days for(i=0;i<100;i++) { or in python for i
in range(99):
应该是`for i in range(100)`来完全匹配C循环。两次迭代
100次,i从0到99不等。
Should be `for i in range(100)` to match exactly the C loop. Both iterate
100 times, with i varying from 0 to 99 inclusive.
> mi***********@gmail.com 写道:
> mi***********@gmail.com wrote:
Ruby在编写循环时有一点点方便,你不需要关注循环索引:你只需要做n次! ...一些
code ...}其中n是一个整数,表示你想要执行多少次
来执行某些代码。
在Python中,这是一个for循环的直接转换。当
指数对我来说无关紧要时,我倾向于把它写成:
$ x $ b for _ in xrange(1,n):
一些代码
Ruby has a neat little convenience when writing loops where you don''t
care about the loop index: you just do n.times do { ... some
code ... } where n is an integer representing how many times you want
to execute "some code."
In Python, the direct translation of this is a for loop. When the
index doesn''t matter to me, I tend to write it as:
for _ in xrange (1,n):
some code
应该是`for _ in xrange(n)`来匹配Ruby示例。两次迭代n $ / b $ b次。
Should be `for _ in xrange(n)` to match the Ruby example. Both iterate n
times.
2008年2月3日上午3:34,Roy Smith< ro*@panix.comwrote:
On Feb 3, 2008 3:34 AM, Roy Smith <ro*@panix.comwrote:
>但是,更重要的是,我会尝试找到变量名称,其中描述了为什么我在循环,即使我没有'实际上使用theloop
体中的值:
>But, more to the point, I''d try to find variable name which described
why I was looping, even if I didn''t actually use the value in theloop
body:
我也是。政府不会按可变名称的数量征税
使用(还)。
-
Gabriel Genellina
Me too. Government don''t collect taxes by the number of variable names
used (yet).
--
Gabriel Genellina
" Gabriel Genellina" < ga ******* @ yahoo.com.arwrites:
"Gabriel Genellina" <ga*******@yahoo.com.arwrites:
应该是`for _ in xrange(n)`来匹配Ruby示例。两个
迭代n次。
Should be `for _ in xrange(n)` to match the Ruby example. Both
iterate n times.
只有在Python 3.0之前,因为那时''xrange''的实现将变成
''range''。 />
< URL:http://wiki.python.org/moin/Py3kDeprecated#head-343618ffa0887790ed12c3f9cf278cd7ca7eef51-2>
-
\预测非常困难,尤其是未来。 |
` \ a ?? Niels Bohr |
_o__)|
Ben Finney
Only until Python 3.0, since the ''xrange'' implementation will become
''range'' at that time.
<URL:http://wiki.python.org/moin/Py3kDeprecated#head-343618ffa0887790ed12c3f9cf278cd7ca7eef51-2>
--
\ "Prediction is very difficult, especially of the future." |
`\ a??Niels Bohr |
_o__) |
Ben Finney
>