On 11/1/17 1:25 PM, Stefan Ram wrote:
I started to collect some code snippets:
Sleep one second
__import__( "time" ).sleep( 1 )
Get current directory
__import__( "os" ).getcwd()
Get a random number
__import__( "random" ).random()
And so on. You get the idea.
However, reportedly, all those snippets are anti-patterns
because they use »__import__«.
But what I like about them: You just paste them in where
you want them to be, and your done.
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 should not optimize for the shortest time to paste a line of code.
You should take time and care writing your code, so that it reads best
and runs best. If you needed another os function, would you have two
__import__("os") in your code? Ugh.
--Ned.
--
https://mail.python.org/mailman/listinfo/python-list