1

Is there a cleaner way to define global variables so that they're all in one line?

Currently I have:

SUCCESS = 0                    # Declarations spanning multiple lines
FAIL = 0
TARGET = os.getcwd()
TIMEOUT = 2
MAXDEPTH = 0
ORIGINAL = ''
PATHS = {}

def main():
   aNumber,aString = 1,'hello' # Declarations in one line
   print aString

main()
2
  • 2
    It's not going to be cleaner. Commented Dec 3, 2014 at 5:53
  • you can separate them with ; Commented Dec 3, 2014 at 5:55

3 Answers 3

4

it's much more readable as separate lines (especially if you line them up):

SUCCESS  = 0
FAIL     = 0
TARGET   = os.getcwd()
TIMEOUT  = 2
MAXDEPTH = 0
ORIGINAL = ''
PATHS    = {}

But if you really want to do it in a single line, sure!

SUCCESS, FAIL, TARGET, TIMEOUT, MAXDEPTH, ORIGINAL, PATHS = 0, 0, os.getcwd(), 2, 0, '', {}
Sign up to request clarification or add additional context in comments.

Comments

2

Put them in a separate .py file (with a sensible name) and then

from other_file import *

Sticking them all on one line is a terrible idea for readability

Comments

2

But it's not a good idea when you have many variables, it's not user readable, it is used when you have few varibles to initialize.

SUCCESS, FAIL, TARGET, TIMEOUT, MAXDEPTH, ORIGINAL, PATHS  = 0, 0, os.getcwd(), 2, 0, '', {}               

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.