没有“副作用”分配! [英] No "side effect" assignment!

查看:60
本文介绍了没有“副作用”分配!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,


我想念我其他语言使用的成语,

类似于:

while(foo = getmore()){

进程(foo);

}

我似乎无法在没有呕吐的情况下在python中执行此操作:


foo = getmore()

而foo:

process(foo)

foo = getmore ()

这是ppl的作用吗?手册sez这是

所以newbs不会这样做:


如果foo = bar:

process( )


认为他们正在检查是否平等。

我觉得python在某些方面受到了餐饮的影响

外行。然而,通常情况下,有很好的解决方法(或者甚至是更好的方法来完成这项工作),

我希望这些工作能够被列表所产生,就像

谢谢,


Tobiah

解决方案

Tobiah写道:


[snip]


<我似乎无法在没有呕吐的情况下在python中这样做:

foo = getmore()
而foo:
进程(foo)
foo = getmore()




然后在下面测试你的呕吐反射:


而1:

foo = getmore()

如果不是foo:

休息

流程(foo)


顺利滑倒我。


[snip]


Dave


- -

Dave Kuhlman
http:// www。 rexx.com/~dkuhlman
dk******@rexx.com


确认!!! (轻微的呕吐反射):)


这实际上是更多的线条,即使更具可读性。


好​​的,我的perl根源是显示,但我正在考虑将以下功能纳入我的标准

模块:


def eq(thingy):

全球_

_ =东西

返回_


完成后,我现在可以说:


而eq(getmore()):

流程(_)

在我看来,添加支持副作用

赋值不会破坏任何现有的脚本。哎呀,

他们为+ =做了这个;)


谢谢,


Tobiah
< blockquote class =post_quotes>然后在下面测试你的呕吐反射:

而1:
foo = getmore()
如果不是foo:
break <过程(foo)

与我滑倒非常顺利。

[snip]

戴夫






" Tobiah" <至** @ rcsreg.com>在消息中写道

新闻:0f ****************************** @ news.teranew s。 com ...

好的,

我想念我的其他语言使用的习语,
类似于:

while(foo) = getmore()){
进程(foo);
}

我似乎无法在没有呕吐的情况下在python中执行此操作:

foo = getmore()
而foo:
进程(foo)
foo = getmore()

这是ppl的作用吗?手册sez,这是
所以newbs不会做:

如果foo = bar:
进程()

认为他们正在检查是否平等。
我觉得python在某些方面会因为不熟悉的问题而受到影响。然而,通常有很好的解决方法(甚至更好的方法来完成这项工作),
我希望这些工作能够被列表所产生,因为它已经记录了它的记录。到目前为止。


虽然Guido的一个原因是不包括作业表达式的分配

是为了避免错误(并且这并不总是

a新手错误,)Python在

''for'语句中提供了类似的功能。在C,C ++和Java中,''for''基本上是一个循环的语法糖,在Python中,它实际上是在
分配下一个元素顺序。


你的例子可能是:

$ get是$ f $ b for getmore:

process(foo)


只要getmore是一个序列或实现

迭代器协议。


这不是总数替换作为表达式的作业,

但它确实覆盖了绝大多数用例。


John Roth


谢谢,

Tobiah



Ok,

I miss the idiom that my other languages use,
something like:
while( foo = getmore()){
process(foo);
}
I can''t seem to do this in python without gagging:

foo = getmore()
while foo:
process(foo)
foo = getmore()
Is that what ppl do? The manual sez that this is
so that newbs won''t do:

if foo = bar:
process()

thinking that they are checking for equality.
I feel that in a few ways python suffers by catering
to the uninitiated. Usually however, there are
great workarounds (or even plain better ways to do the job),
which I am hopeful will be yielded by the list, as has
been it''s record thus far to do.

Thanks,

Tobiah

解决方案

Tobiah wrote:

[snip]



I can''t seem to do this in python without gagging:

foo = getmore()
while foo:
process(foo)
foo = getmore()



Then test your gag reflex on the following:

while 1:
foo = getmore()
if not foo:
break
process(foo)

Slips down pretty smoothly with me.

[snip]

Dave

--
Dave Kuhlman
http://www.rexx.com/~dkuhlman
dk******@rexx.com


Ack!!! (slight gag reflex) :)

That''s actually more lines, even if more readable.

Ok, my perl roots are showing, but I was thinking
of putting the following function into my standard
module:

def eq(thingy):
global _
_ = thingy
return _

Having done that, I can now say:

while eq(getmore()):
process(_)
It seems to me that adding support for the side effect
assignment would not break any existing scripts. Gee,
they did it for += ;)

Thanks,

Tobiah

Then test your gag reflex on the following:

while 1:
foo = getmore()
if not foo:
break
process(foo)

Slips down pretty smoothly with me.

[snip]

Dave





"Tobiah" <to**@rcsreg.com> wrote in message
news:0f******************************@news.teranew s.com...

Ok,

I miss the idiom that my other languages use,
something like:
while( foo = getmore()){
process(foo);
}
I can''t seem to do this in python without gagging:

foo = getmore()
while foo:
process(foo)
foo = getmore()
Is that what ppl do? The manual sez that this is
so that newbs won''t do:

if foo = bar:
process()

thinking that they are checking for equality.
I feel that in a few ways python suffers by catering
to the uninitiated. Usually however, there are
great workarounds (or even plain better ways to do the job),
which I am hopeful will be yielded by the list, as has
been it''s record thus far to do.
While one of Guido''s reasons for not including assignment
as an expression was to avoid errors (and that''s not always
a novice error,) Python provides similar functionality in the
''for'' statement. In C, C++ and Java, ''for'' is basically
syntactic sugar around a loop, in Python, it actually does
an assignment of the next element of a sequence.

Your example could be:

for foo in getmore:
process(foo)

as long as getmore is either a sequence or implements the
iterator protocol.

This isn''t a total replacement for assignment as an expression,
but it does cover the overwhelming majority of use cases.

John Roth


Thanks,

Tobiah



这篇关于没有“副作用”分配!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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