Code Snippets

Steve D'Aprano steve+python at pearwood.info
Wed Nov 1 21:21:59 EDT 2017


On Thu, 2 Nov 2017 08:02 am, Ben Bacarisse wrote:

> ram at zedat.fu-berlin.de (Stefan Ram) writes:
> 
>> Wolfgang Maier <wolfgang.maier at biologie.uni-freiburg.de> writes:
>>>If you're worried bout having things on separate lines, you could write:
>>>import os; os.getcwd()
>>>,etc., which is actually saving a few characters :)
>>
>>   Yes, but there still is the risk of the identifier »os«
>>   already being used in the sorrounding code. While
>>
>> __import__( "os" ).getcwd()
>>
>>   does not seem to "leak" names into the enclosing scope.
> 
> Also it's an expression which may be important in your "quick and dirty"
> scripts.

No script is so quick or so dirty to justify calling __import__ with a string
literal argument instead of import.

We've all written quick and dirty throw-away scripts where we don't care too
much about best practices. But this isn't so much less-than-best practices as
worst-practices: optimizing to save a few seconds during the initial editing
run, by using the copy-and-paste anti-pattern.



-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list