S
S
seosova2015-11-24 15:43:20
Python
seosova, 2015-11-24 15:43:20

Should global variables be written in capital letters in regular scripts?

There is for example the simplest script from Lutz. If you run it through pylint, it swears that the variables are global and let's write them large. But this kind of script, for some reason, cuts the eye very much

#!/usr/bin/env python3
""" Small test program for while cycle """

while True:
    REPLY = input('Enter text:')
    if REPLY == 'stop':
        break
    try:
        NUM = int(REPLY)
    except:
        print('Bad!' * 8)
    else:
        if NUM < 20:
            print('low')
        else:
            print(int(REPLY) ** 2)
print('Bye!')

Maybe it's worth just "score" on the pickiness of pylint (especially pep8, flake8 normally perceive this). Or, by and large, even simple scripts, and just variables, but existing at the level of the entire script, should be written in large as "really" global ones?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
angru, 2015-11-24
@angru

What if this block of code is wrapped in:

def main():
    while True:
        # do something

if __name__ == '__main__':
    main()

Still nagging? For example, I have never done this, it is implied (by me) that if a variable is declared at the very top level, then yes - it is global, it must contain only capital letters and cannot be changed anywhere in the code (again, all this is based on agreements). You have them declared in the while block, and even change, it would mislead me, because It looks like a constant.
PS In its current form, your code contradicts PEP-8.

N
nirvimel, 2015-11-24
@nirvimel

Upper case constants.
Variables are at the bottom.

R
Roman Kitaev, 2015-11-24
@deliro

REPLY = input('Enter text:')
This variable, by definition, does not fit a constant, therefore, it is an ordinary variable and it is still worth writing as_ordinary_variable.

S
s0ci0pat, 2015-11-24
@s0ci0pat

If you don't get used to the code style now, then it will be too late.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question