r/Python • • Apr 25 '21

Tutorial Stop hardcoding and start using config files instead, it takes very little effort with configparser

We all have a tendency to make assumptions and hardcode these assumptions in the code ("it's ok.. I'll get to it later"). What happens later? You move on to the next thing and the hardcode stays there forever. "It's ok, I'll document it.. " - yeah, right!

There's a great package called ConfigParser which you can use which simplifies creating config files (like the windows .ini files) so that it takes as much effort as hardcoding! You can get into the hang of using that instead and it should both help your code more scalable, AND help with making your code a bit more maintainble as well (it'll force you to have better config paramters names)

Here's a post I wrote about how to use configparser:

https://pythonhowtoprogram.com/how-to-use-configparser-for-configuration-files-in-python-3/

If you have other hacks about managing code maintenance, documentation.. please let me know! I'm always trying to learn better ways

1.5k Upvotes

323 comments sorted by

View all comments

Show parent comments

6

u/POTUS Apr 25 '21

You choose which file to import, but you don’t control what that file does. If the file wants to

os.system(‘rm -rf /usr’)

You can put that in a config.py file, and it will run. If you put it in an ini or json or yaml file, it’s just a bit of text.

2

u/[deleted] Apr 25 '21

I had to check out your account. A name like POTUS had to be taken in the early days of Reddit. Sure enough, a 12 year old account.

Glad to have ran across ya. Be well.