[Python-Dev] Stupid Python Tricks, Volume 38 Number 1

Guido van Rossum guido@digicool.com
2001年1月20日 11:43:49 -0500


> I had a huge string and wanted to put a double-quote on each end. The
> boring:
>> '"' + huge + '"'
>> does the job, but is inefficent <snort>. Then this transparent variation
> sprang unbidden from my hoary brow:
>> huge.join('""')

Points off for obscurity though! My favorite for this is:
 '"%s"' % huge
Worth a microbenchmark?
> *That* should put to rest the argument over whether .join() is more properly
> a method of the separator or the sequence -- '""'.join(huge) instead would
> look plain silly <wink>.
>> not-entirely-sure-i'm-channeling-on-this-one-ly y'rs - tim

Give up the channeling for a while -- there's too much interference in
the air from the Microsoft threaded stdio debate still. :-)
--Guido van Rossum (home page: http://www.python.org/~guido/)

AltStyle によって変換されたページ (->オリジナル) /