對於你的第一個例子中,我使用的形式是這樣的:
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