对于你的第一个例子中,我使用的形式是这样的:
print '\n'.join(obj.name for obj in iterable)
而且我会用:
firstnotvalid = next(obj.name for obj in iterable if not obj.is_valid())
,而不是和:
>>> import sys
>>> map(lambda n: None if n % 2 else sys.stdout.write(str(n)), range(1, 100))
2468101214161820222426283032343638404244464850525456586062646668707274767880828486889092949698[None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]
我会做:
>>> print (', '.join(str(number) for number in range(1,100) if not number % 2))
2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98
忽略不存在的一系列步骤参数,因为我认为功能是其他更复杂的功能简化。
-1:哇,这一切都很糟糕。请不要玩这样的代码高尔夫球。请不要让Python无法读取。第一种风格有什么问题?这是有道理的。为什么使用让事物变得模糊的奇怪的地图技术?这些默默无闻的重点是什么?它不会更快。阅读起来并不容易。为什么要这样做? – 2010-09-17 10:05:14
@S。洛特前两个地图的例子是无用的,但他们只是演示sys.stoud.write和我想用raise的方式。但是我不明白为什么第三张地图可能会坏。 – cory 2010-09-17 15:53:34
为什么在使用'from __future__ import print_function'时使用'sys.stdout.write'? – 2010-09-17 17:28:58