问题描述
我肯定一定有关于这个主题的过去主题,但我不知道如何找到它:
如何扩展for< X>在"语法,以便X可以包含默认参数?这对于列表/生成器理解非常有用,例如能够写出如下内容:
[x * yz for(x,y, z = 0)in(1,2,3),(4,5),(6,7,8)]
而不是不那么优雅的显式循环版本检查每个序列的长度。
您怎么看?
George
I''m sure there must have been a past thread about this topic but I don''t know how to find it: How
about extending the "for <X> in" syntax so that X can include default arguments ? This would be very
useful for list/generator comprehensions, for example being able to write something like:
[x*y-z for (x,y,z=0) in (1,2,3), (4,5), (6,7,8)]
instead of the less elegant explicit loop version that has to check for the length of each sequence.
What do you think ?
George
推荐答案
看起来非常吸引人,但是如何处理
[x * yz for(x = 0,y,z)in(1,2,3), (4,5),(6,7,8)]?
由于模式不匹配会引发异常吗?
如果没有,匹配规则应该如何应用?
[x * yz for(x = 0,y = 0,z = 0)in(1,2,3),(4 ,5),(6,7, 8)]
如果有疑问,请写一个关心正确填充的矢量类(
或更多一般和具有几何意义:选择正确的超平面
)并允许在不同的填充之间切换。这个解决方案是灵活的和可重复使用的
。
问候凯
Looks very appealing, but what to do with
[x*y-z for (x=0,y,z) in (1,2,3), (4,5), (6,7,8)] ?
Should it raise an exception due to a pattern mismatch?
If not how should matching rules apply here?
[x*y-z for (x=0,y=0,z=0) in (1,2,3), (4,5), (6,7,8)]
If in doubt write a vector class that cares about the correct padding (
or more general and with geometric meaning: select the right hyperplane
) and enable to switch between different paddings. This solution is
both flexible and reusable.
Regards Kay
您是如何获得该格式的数据的?它看起来对我来说有点奇怪。
你收集数据而不是使用它时,填写默认值会不会更容易?
-
Ciao,
Matteo
How did you get the data in that format in the first place? It looks a
bit strange to me. Wouldn''t it be easier to fill in default values when
you gather data as opposed to when you use it?
--
Ciao,
Matteo
看起来非常吸引人,但是如何处理
[x * yz for(x = 0,y,z)in(1, 2,3),(4,5),(6,7,8)]?
由于模式不匹配会引发异常吗?
Looks very appealing, but what to do with
[x*y-z for (x=0,y,z) in (1,2,3), (4,5), (6,7,8)] ?
Should it raise an exception due to a pattern mismatch?
>
我没有想到将语法概括为比函数
签名更多,因此这将是语法错误:
SyntaxError :关键字arg之后的非关键字arg
如果不是这里应该如何应用匹配规则?
[x * yz for(x = 0,y = 0,z = 0 )(1,2,3),(4,5),(6,7,8)]
如果有疑问,请写一个关心正确填充的矢量类(
或更一般和几何mea ning:选择正确的超平面
并启用不同填充之间的切换。这个解决方案既灵活又可重复使用。
关心Kay
I didn''t have in mind to generalize the syntax even more than the respective for function
signatures, therefore this would be syntax error:
SyntaxError: non-keyword arg after keyword arg
If not how should matching rules apply here?
[x*y-z for (x=0,y=0,z=0) in (1,2,3), (4,5), (6,7,8)]
If in doubt write a vector class that cares about the correct padding (
or more general and with geometric meaning: select the right hyperplane
) and enable to switch between different paddings. This solution is
both flexible and reusable.
Regards Kay
这只是一个例子;我认为所建议的功能在处理几何或向量的情况下将比有用的更有用,所以我希望它能得到语言本身的支持。
问候,
乔治
This was just an example; I think the proposed functionality will be helpful in far more cases than
dealing with geometry or vectors, so I would prefer it to be supported by the language itself.
Regards,
George
这篇关于用于循环扩展语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!