关于PEP284的想法 [英] Thoughts on PEP284

查看:56
本文介绍了关于PEP284的想法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



更多循环思路 - 这次是循环整数...


可能会有一些提示PEP284(循环整数)一个评论

来自其他语言的想法,但如果我能搞清楚的话,我该死的。

我花了一些时间思考它并且找不到什么

将涵盖这个问题。


我想到的是其他一些语言的认可

''用于''和'''foreach''循环。但是很久以前,Python继续使用

''来代替循环列表/迭代器。也许一个新的

关键字或组合可能会有所帮助,例如...


范围i在0< = i< 10:


或...


的范围0< = i< 10:


或放弃PEP284概念而转向基于C的东西...


for i yield i = 0;我< 10;我+ = 1:


但是我并不是很喜欢这些,说实话。

我确实想出了一些东西。 ..


我怀疑是否需要整数循环的独占范围。我还怀疑是否需要在不同系列系统之间切换

(包括,独占,半开)。 IMO比那些路线上的任何东西更加混乱。


虽然我没有遇到包容性循环的问题(Basic,Ada)我的首选项是半开循环。并且

我发现Python程序员定期在

半开放方式中指定整数范围 - 在列表上进行切片和

字符串。


也许''int''和''long''类型可以支持切片生成

a列表或迭代器范围?


如果是这样的话,我们可以写一下...


for int in int [0:10]:

...


如果语法创建了一个迭代器而不是一个列表,它甚至可以按如下方式使用
...


for i in long [0:]:

...

如果< condition> :

...


我们可以有一些基本的步骤循环...


for i,j in zip(int [0:],int [len(list)-1 :: - 1]:

...

如果i> b中断l:

...


但这可能是反对的理由。


什么会好,但是,它可以在

循环之外用于免费 - 列表推导,例如......


[i * i for i in int [0:10]]


与普通切片相比,它需要一些轻微的限制

(它需要知道要开始的值,即使不是在哪里停止)

但是到底是什么。至少我不会被指责试图将b $ b变成另一种语言--AFAIK这片-a型的想法是原来的。这可能是我第一次被指责试图将Python变成一种当然不存在的语言;-)

-

Steve Horne


steve at ninereeds dot fsnet dot co dot uk

解决方案

" Stephen Horne" <

@

.co.uk>写在

消息新闻:5j ******************************** @ 4ax.com ...


一些循环的想法 - 这次是循环整数...




嗨。

以下内容如何:


for i in 0 ... 10:

#suite


其中0 ... 10的作用类似于xrange(10)。所以你得到i = 0然后1然后2

....然后9.要获得0到10,包括在内,你需要说0 ... 11(Ruby已经

0..10,但我怀疑这会在这里通过鼓励。不幸的是,这个建议,
不提供步骤控制,除了通过关键字,我不能想出一个干净的

方式来介绍它:


for i in 0 ... 10 by 2:

#suite


也将被击落,我怀疑,我将在0到10之间获得



for i in 0 to 10 by 2:





也许生成器可以增加一个by()方法,这样你就可以控制你通过迭代步骤的方式了解b / b
像这样:


for i in 0 ... 10.by(2):

#suite


无论如何。


将省略号放在一边,你可以为int / long添加方法(也是ala

Ruby),


for i in 0.upto(10):

#suite


#倒数

for i在10.downto(0):

#suite


其中方法upto()和downto()是带有可选步骤的生成器

辩论nt,即


def upto(self,stop,step = 1):

#suite

并且,毕竟那么,你可以说,我们已经拥有了


我的范围(10):

#suite


如果只有范围是一个生成器函数...但是这会破坏代码,你可以使用xrange()来使用
,有一天它会(可能),并且...... ,和...,yada,yada,

yada。


无论如何。



Some more looping thoughts - this time on integer for loops...

There may be some hint towards PEP284 (integer for loops) in a review
of ideas from other languages, but I''m damned if i can figure it out.
I spent some time thinking about it and couldn''t find anything that
would cover the issue.

All I came up with was the recognition that some other languages have
''for'' and ''foreach'' loops. But Python went down the route of using
''for'' for looping over lists/iterators a long time ago. Maybe a new
keyword or combination could help, such as...

for range i in 0 <= i < 10 :

or...

for range 0 <= i < 10 :

or dropping the PEP284 concept in favor of something C based...

for i yield i = 0; i < 10; i += 1 :

but I don''t much like any of those, to be honest.
I did come up with something, though...

I doubt the need for exclusive ranges in integer for loops. I also
doubt the need for switching between different range systems
(inclusive, exclusive, half-open). IMO there is more confusion than
anything down those routes.

Although I haven''t had problems with inclusive loops (Basic, Ada) my
preference is toward half-open loops if a choice needs to be made. And
it occurs to me that Python programmers do specify integer ranges in a
half-open way on a regular basis - when doing slicing on lists and
strings.

Perhaps the ''int'' and ''long'' types could support a slicing to generate
a list or iterator over the range?

If so, we could write...

for i in int[0:10] :
...

If the syntax creates an iterator rather than a list, it could even be
used as follows...

for i in long[0:] :
...
break if <condition> :
...

We could have some basic in-step looping with...

for i, j in zip(int[0:], int[len(list)-1::-1] :
...
break if i > l :
...

but this is probably an argument against.

What would be good, however, is that it can be useful outside of
looping for free - list comprehensions, for instance...

[i*i for i in int[0:10]]

It would need some slight restrictions compared with normal slicing
(it needs to know the value to start at, even if not where to stop)
but what the hell. At least I''m not going to be accused of trying to
turn Python into another language - AFAIK this slice-of-a-type idea is
original. This could be my first time being accused of trying to turn
Python into a language that doesn''t exist yet, of course ;-)
--
Steve Horne

steve at ninereeds dot fsnet dot co dot uk

解决方案

"Stephen Horne" <


@


.co.uk> wrote in
message news:5j********************************@4ax.com...


Some more looping thoughts - this time on integer for loops...



Hi.
How about the following:

for i in 0...10:
# suite

where 0...10 works something like xrange(10). So you get i=0 then 1 then 2
.... then 9. To get 0 thru 10, inclusive, you''d need to say 0...11 (Ruby has
0..10, but I doubt that''d pass muster here). This suggestion,
unfortunately, does not provide step control and I can''t think of a clean
way to introduce it other than via keyword:

for i in 0...10 by 2:
# suite

which will also be shot down, I suspect, as would

for i in 0 to 10 by 2:

for i in 0 thru 10 by 2:

etc.

Perhaps generators could grow a by() method so that you could control how
you step thru the iteration, something like this:

for i in 0...10.by(2):
# suite

Whatever.

Leaving the ellipsis aside, you could add methods to int/long (also ala
Ruby),

for i in 0.upto(10):
# suite

# to count down
for i in 10.downto(0):
# suite

where the methods upto() and downto() are generators with an optional step
argument, i.e.

def upto(self, stop, step=1):
# suite
And, after all of that, you could say, well we already have

for i in range(10):
# suite

If only range was a generator function...but that''d break code, and you can
use xrange(), and some day it will(may) be, and ..., and ..., yada, yada,
yada.

Whatever.


这篇关于关于PEP284的想法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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