Niklas Rosencrantz
Niklas Rosencrantz

Reputation: 26655

Constant variable even though it changes

Please consider this code after running pylint:

'''
Test
'''
i = 0
while i < 4:
    myvar = i
    i = i + 1

pylint reports: Constant name "myvar" doesn't conform to UPPER_CASE naming style (invalid-name) But how much is myvar really a constant, when it obviously changes during the running process?

IIUC, it is not a false positive but rather myvar is regarded being a constant because it never changes during the iteration, and the next iteration the variable is regarded as "new". Did I understand it correctly?

Upvotes: 1

Views: 384

Answers (1)

Mikhail Burshteyn
Mikhail Burshteyn

Reputation: 5002

Pylint thinks that myvar is a constant by convention, because it is global (declared on module level).

Generally, you should not be writing code like this on a module level, wrap it in a function instead:

def main():
    i = 0
    while i < 4:
        myvar = i
        i = i + 1

if __name__ == '__main__':
    main()

Upvotes: 2

Related Questions