On 2017-11-01, Stefan Ram <r...@zedat.fu-berlin.de> wrote: > I started to collect some code snippets: > > Sleep one second > > __import__( "time" ).sleep( 1 ) > > What I'm supposed to do instead, I guess, is: > > Sleep one second > > import time > ... > time.sleep( 1 ) > > Get current directory > > import os > ... > os.getcwd() > > Get a random number > > import random > ... > random.random() > > Now, the user has to cut the import, paste it to the top > of his code, then go back to the list of snippets, find > the same snippet again, copy the expression, go to his code, > then find the point where he wanted to insert the snippet again, > and finally insert the snippet. And still there now is a > risk of name collisions. So, it seems to me that __import__ > is just so much better!
You can import wherever you like--only good style requires you to put them at the top of your file. Moreover, snippets could be a library, with each snippet a function, with the import inside the function. That would keep the module name out of your global namespace. -- Neil Cerutti -- https://mail.python.org/mailman/listinfo/python-list