python基本while循环

Wes*_*Wes 1 python while-loop

我有另一个新手Python问题.我有以下一段代码,我有一种感觉,不是应该是pythonic写的:

    rowindex = 0
    while params.getfirst('myfield'+rowindex):
        myid = params.getfirst('myfield'+rowindex)
        # do stuff with myid
        rowindex+=1
Run Code Online (Sandbox Code Playgroud)

此脚本的输入是一个HTML页面,可以包含任意数量的名为"myfield#"的输入字段,其中#从0开始并按顺序增加.在Perl中,我会做更像这样的事情:

    rowindex = 0
    while myid =  params.getfirst('myfield'+rowindex):
        #do stuff with myid
        rowindex+=1
Run Code Online (Sandbox Code Playgroud)

但这不是Python中的有效语法.我知道我的工作会有什么用,但是有更好的方法吗?谢谢.

S.L*_*ott 5

简单计数器的"无界"性质具有一定的吸引力.但是,这也是某人通过欺骗具有数十亿字段的表单来尝试拒绝服务攻击的机会.当您尝试处理这些数十亿个字段时,只需计算字段就可以阻止您的Web服务器.

由于Python自动转换为long,因此通常的20亿整数溢出不适用.有人可以用数十亿的领域惩罚你的网站.

for i in range(1024): # some sensible upper limit, beyond which the input is suspicious
    myid= params.getfirst("myfield%d" % i)
    if not myid: break
        # do stuff with myid
Run Code Online (Sandbox Code Playgroud)