Code Snippets

Chris Angelico rosuav at gmail.com
Wed Nov 1 17:11:48 EDT 2017


On Thu, Nov 2, 2017 at 8:02 AM, Ben Bacarisse <ben.usenet at bsb.me.uk> 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.

If your quick-and-dirties are needing these kinds of imports all the
time, the best solution might be to slap something into site.py that
"pre-imports" those into the builtins. Then you can just use
os.getcwd() without worrying about the import, and without calling a
dunder.

ChrisA



More information about the Python-list mailing list