python3:'where'关键字 [英] python3: 'where' keyword

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

问题描述

嗨。


能够通过在哪里来反转使用/定义部件,这将很棒。

haskell-way关键词。由于Python 3会错过lambda,因此

对于创建可读源非常有用。


用法可能类似于:

Hi.

It would be great to be able to reverse usage/definition parts in
haskell-way with "where" keyword. Since Python 3 would miss lambda, that
would be extremly useful for creating readable sources.

Usage could be something like:

res = [f(i)for i in objects]其中:
def f(x):
#做点什么




打印单词[3],单词[5]其中:
words = input.split()


- 在where中定义变量block会限制他们的可见度

一个表达式


- 当你知道哪个部分可以跳过时,它更容易阅读来源,
比较

def f(x):
#do something
res = [f(i)for i in objects]
res = [ f(i) for i in objects ] where:
def f(x):
#do something
or
print words[3], words[5] where:
words = input.split()
- defining variables in "where" block would restrict their visibility to
one expression

- it''s more easy to read sources when you know which part you can skip,
compare to
def f(x):
#do something
res = [ f(i) for i in objects ]




在这种情况下你读到了f的定义在你知道

之前的用法。



in this case you read definition of "f" before you know something about
it usage.

推荐答案

Andrey Tatarinov写道:
Andrey Tatarinov wrote:
你好。

能够在
haskell-way中使用where来反转使用/定义部分会很棒。关键词。由于Python 3会错过lambda,因此对于创建可读源非常有用。

用法可能类似于:
Hi.

It would be great to be able to reverse usage/definition parts in
haskell-way with "where" keyword. Since Python 3 would miss lambda, that
would be extremly useful for creating readable sources.

Usage could be something like:
>>> res = [f(i)for i in objects]其中:
>>> def f(x):
>>> #do something

>>>打印单词[3],单词[5]其中:
>>> words = input.split()
- 在where中定义变量block会限制他们的可见度
一个表达式


多久这个真的有必要?你能描述一下

的一些好处吗?我想我唯一一次遇到范围问题就是

lambda,例如


[lambda x:f(x)代表x, f in lst]


而不是


[lambda x,f = f:for x,f in lst]


还有其他情况会遇到这些问题吗?

- 当你知道哪些部分可以跳过时,它更容易阅读来源,
比较
>>> def f(x):
>>> #do something
>>> res = [f(i)for i in objects]
>>> res = [ f(i) for i in objects ] where:
>>> def f(x):
>>> #do something
or
>>> print words[3], words[5] where:
>>> words = input.split()
- defining variables in "where" block would restrict their visibility to
one expression
How often is this really necessary? Could you describe some benefits of
this? I think the only time I''ve ever run into scoping problems is with
lambda, e.g.

[lambda x: f(x) for x, f in lst]

instead of

[lambda x, f=f: for x, f in lst]

Are there other situations where you run into these kinds of problems?
- it''s more easy to read sources when you know which part you can skip,
compare to
>>> def f(x):
>>> #do something
>>> res = [ f(i) for i in objects ]



在这种情况下,您阅读了f的定义。在您了解它的用法之前。



in this case you read definition of "f" before you know something about
it usage.




嗯...这似乎是个人偏好的问题。我发现

你的where子句让我跳过'res'的赋值来读取'res''块包含的
。在我实际看到列表理解之前,我必须阅读它两次。当然,我确定我可以接受再培训以正确的方式阅读,但直到我看到一些真正的好处

,我宁愿没有。

TOOWTDI-ily-yrs,


Steve



Hmm... This seems very heavily a matter of personal preference. I find
that your where clause makes me skip the ''res'' assignment to read what
the ''res'' block contains. I had to read it twice before I actually
looked at the list comprehension. Of course, I''m sure I could be
retrained to read it the right way, but until I see some real benefit
from it, I''d rather not have to.
TOOWTDI-ily-yrs,

Steve


Steven Bethard写道:
Steven Bethard wrote:
这经常需要多久一次?你能描述一下这个好处吗?我认为我唯一遇到范围问题的时候是
lambda,例如

[lambda x:f(x)代表x,f代表lst]

而不是

[lambda x,f = f:for x,f in lst]
How often is this really necessary? Could you describe some benefits of
this? I think the only time I''ve ever run into scoping problems is with
lambda, e.g.

[lambda x: f(x) for x, f in lst]

instead of

[lambda x, f=f: for x, f in lst]




对不起,不好的例子,这看起来应该更像:


[lambda y:f(x,y)代表x,f代表lst]


。 ......


[lambda y,x = x,f = f:f(x,y)代表x,f代表lst]


你真正需要lambda。


Steve



Sorry, bad example, this should have looked something more like:

[lambda y: f(x, y) for x, f in lst]

....

[lambda y, x=x, f=f: f(x, y) for x, f in lst]

where you actually need the lambda.

Steve


文章< xd ******** ************@comcast.com> ;,

Steven Bethard< st ************ @ gmail.com>写道:
In article <xd********************@comcast.com>,
Steven Bethard <st************@gmail.com> wrote:
Andrey Tatarinov写道:
Andrey Tatarinov wrote:


能够反转使用/定义会很棒
haskell-way中的部分带有where的部分关键词。由于Python 3会错过lambda,因此对于创建可读源非常有用。

用法可能类似于:
It would be great to be able to reverse usage/definition parts in
haskell-way with "where" keyword. Since Python 3 would miss lambda, that
would be extremly useful for creating readable sources.

Usage could be something like:
>> ;> res = [f(i)for i in objects]其中:
>>> def f(x):
>>> #do something
>>> res = [ f(i) for i in objects ] where:
>>> def f(x):
>>> #do something






or

>>>打印单词[3],单词[5]其中:
>>> words = input.split()
>>> print words[3], words[5] where:
>>> words = input.split()



- 在where中定义变量block会限制他们对一个表达式的可见性



- defining variables in "where" block would restrict their visibility to
one expression



这经常需要多久一次?你能描述一下这个好处吗?我认为我唯一遇到范围问题的时候是
lambda,例如

[lambda x:f(x)代表x,f代表lst]

而不是

[lambda x,f = f:for x,f in lst]

还有其他情况会遇到这些问题吗?



How often is this really necessary? Could you describe some benefits of
this? I think the only time I''ve ever run into scoping problems is with
lambda, e.g.

[lambda x: f(x) for x, f in lst]

instead of

[lambda x, f=f: for x, f in lst]

Are there other situations where you run into these kinds of problems?




请注意,他说对于创建可读的

来源非常有用,所以这些类型的问题也是如此。他一直在想

将是源不尽可能可读的地方。你似乎需要关注其他事情。


我绝不同意这种表示法值得采用,并且
一般来说,
我认为这种可读性问题或多或少是因为使用Python的范围规则的语言而导致的损失,但动机使得
$ b $对我有意义。一种看待它的方法可能是,如果我观察到单词

被分配到where子句中,那么我知道它不会被使用

其他地方在周围的范围内,所以我可以马上忘记它。

如果名字确实发生在其他地方,它显然是指别的东西。


Donn Cave, do**@u.washington.edu


这篇关于python3:'where'关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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