Search code examples
pythoncommand-line-arguments

Python append to system arguments - bad practice?


I'm parsing system arguments in my Python project using sys.argv. At some point i had to modify the script after having written the logic that parses system args. I added a line the basically appends a string to sys.argv so the logic that parses it won't be changed -

sys.argv.append('some string here')

Is it a bad practice to modify the system arguments after they have been created for the program ?


Solution

  • It is bad practice to modify sys.argv in Python and it's equivalent in other languages.

    In these situations I recommend a parsed_args variable which has all your parsed data from sys.argv, any default values that you would like to set, and any modifications that "middleware" would make.