This isn't really much of an actual project, just something I threw together in about 5-10 minutes. I just want to know if it goes with the current Python standards, and if anything should be changed or fixed.

print(''.join([__import__('random').choice(['\033[7;1;32m  \033[0m','\033[7;32m  \033[0m','\033[7;1;33m  \033[0m','\033[7;34m  \033[0m']) for world in range(40)])); __import__('time').sleep(0.5)

Can anything be improved? What needs to be fixed?


closed as off-topic by Dannnno, Stephen Rauch, Daniel, yuri, Mast Aug 5 '18 at 13:45

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Dannnno, Stephen Rauch, Daniel, yuri, Mast
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    \$\begingroup\$ Is it a requirement that it is a one-liner (code-wise, not output-wise), and if so, why? \$\endgroup\$ – Jonas Schäfer Jun 23 '14 at 15:09
  • \$\begingroup\$ @JonasWielicki I wrote this as a challenge for myself, so my general preference is that it stays at one line. If it goes against standards and has to be on more than one line, I'm okay with that too. \$\endgroup\$ – Vladimir Putin Jun 23 '14 at 15:11
  • 2
    \$\begingroup\$ I think it looks fancier in a loop. :D \$\endgroup\$ – Doorknob Jun 24 '14 at 0:11
  • \$\begingroup\$ @Doorknob Nice! Unutbu? \$\endgroup\$ – Vladimir Putin Jun 24 '14 at 2:05
  • 1
    \$\begingroup\$ Yes, I'm running Ubuntu. (I just added a for __ in range(99): to the beginning. :-P) \$\endgroup\$ – Doorknob Jun 24 '14 at 3:39

What does THE standard say about imports :

Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.

What else could easily be improved in your code ?

  • _ is a usual name for variable whose value is not going to be used.
  • you do not need to create a list : you can use generator expressions by getting rid of brackets.
  • you can get rid of the duplicated prefix and suffix in the different choices.
  • in Python 2.*, you do not need parenthesis to call print.

Your code would become :

from random import choice
from time import sleep
print ''.join('\033[7;' + choice(['1;32','32','1;33','34']) + 'm  \033[0m' for _ in range(40))
  • \$\begingroup\$ Usually when I do complex printing like this I prefer to use print() instead of print. I don't know why, I just do. \$\endgroup\$ – Vladimir Putin Jun 23 '14 at 15:34
  • \$\begingroup\$ It’s also more forward-compatible with python3 to do that. \$\endgroup\$ – Jonas Schäfer Jun 23 '14 at 15:46
  • \$\begingroup\$ Well, I assumed that the point was to make things as short as possible. Removing 2 parenthesis and adding 1 space = WIN \$\endgroup\$ – SylvainD Jun 23 '14 at 15:51

Of course, you’re violating the 79 characters-per-line limit as per PEP 8, but I guess you know that and thats hard to avoid when you want to stay on one line.

  • You can save some characters and some memory by using generator expressions:

    ''.join([random.choice(…) for world in range(40)]) 

    then becomes

    ''.join(random.choice(…) for world in range(40)) 

    This prevents that the whole list is created in memory before being joined, and is also more readable, as it saves brackets.

  • Using __import__ is not exactly every-day standard. Unfortunately, there is no other way for handling this in one line. Preferably, you would be using at least three lines:

    import random
    import time  # only if you need the delay
    print(''.join([random.choice(['\033[7;1;32m  \033[0m','\033[7;32m  \033[0m','\033[7;1;33m  \033[0m','\033[7;34m  \033[0m']) for world in range(40)])); time.sleep(0.5)

    One issue with __import__ is that it returns the top-level package module instead of the actual module. So __import__("os.path") returns the os module instead of the os.path module, which can be surprising to some and which is why one should avoid using it.

    The suggested alternative is importlib.import_module, but you cannot use this of course unless you first import importlib, defeating the purpose.

There are several other readability issues, but these all relate to the fact that you want to stay on one line, so they’re out of scope for this question.

  • \$\begingroup\$ I thought it was 80 chars per line. \$\endgroup\$ – Vladimir Putin Jun 23 '14 at 15:37
  • \$\begingroup\$ @SomeGuy As said in the document, it should be 79 characters (quoting) “to avoid wrapping in editors with the window width set to 80, even if the tool places a marker glyph in the final column when wrapping lines. \$\endgroup\$ – Jonas Schäfer Jun 23 '14 at 15:47
  • \$\begingroup\$ @SomeGuy Meh, you're lucky, limit is 72 characters on punch-card. \$\endgroup\$ – rolfl Jun 23 '14 at 23:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.