while (a=b()) ...
Tim Peters
tim_one at email.msn.com
Wed May 12 03:02:12 EDT 1999
[/F]
> ...
> now, has anyone written any useful Python code today?
[Steven D. Majewsk]
> OK -- new suggested idiom for those folks who Really, Really have
> to have it all in one statement (It *could* be on one line, but you
> wouldn't be able to read it):
>
>
> import sys,string
>
> while setattr( sys.modules[__name__], 'LINE',
> getattr( sys.modules[__name__], 'FILE',
> setattr( sys.modules[__name__], 'FILE',
> getattr( sys.modules[__name__], 'FILE',
> open('ReadMe')))).readline()) or LINE: print \
> string.strip(LINE)
Steven, Steven, Steven, this opens the file again for every line! There's
no need to sacrifice efficiency for elegance -- not in Python. Note how
much clearer this variant is, yet opening the file just once, and avoiding
those pesky getattr calls:
while setattr(sys.modules[__name__],
'LINE',
(hasattr(sys.modules[__name__], 'FILE') and FILE
or (setattr(sys.modules[__name__],
'FILE',
open('ReadMe')
)
or FILE)
).readline()
) or LINE: print string.strip(LINE)
The only further improvement I can think of is to introduce $ as a special
identifier, short for sys.module[__name__].
and-maybe- at -for-string.strip-while-we're-at-it<wink>-ly y'rs - tim
More information about the Python-list
mailing list