Search code examples
pythonconstantsmutability

Are mutable constants safe?


Are there any sort of 'gotchas' associated with having a constant that is a list, or any other mutable object?

Currently my context has to do with constants that will be passed to a call, but I ask the question generically as I don't feel that topic impacts the question meaningfully.

Consider this example code:

#!/usr/bin/env python
# This file was not tested before posting.

import subprocess

LS_FLAGS = ['-l', '-a']

def main():
    subprocess.call(['ls'] + LS_FLAGS)

if __name__ == '__main__':
    main()

I ask this because I am deathly aware of the problems that an arise from mutable objects in function definitions; And, even as I understand that there should never be anything doing assignment or mutation to a respected constant; and that "constants" are not really a thing: I ask, and further ask, might there be some conventions for semantically protecting ones self again't accidental mutations?


Solution

  • Not having to worry about mutation can make code easier to reason about, and as such, it's one of the core tenets of pure functional programming.

    In this specific case, it doesn't seem like it would be a serious issue, and might even be argued to be a feature. You can always define LS_FLAGS as a tuple if you feel there is no reason for it to be modifiable, but the user of your code can always redeclare LS_FLAGS entirely if they like.