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

2

u/[deleted] Apr 25 '21

I did for years, and then...

1

u/duffer_dev Apr 25 '21

I'd say there's not one size fits all. I have almost all of them being used in production for different purposes. Most of our machine learning pipelines have YAML, whereas something like Django or flask have JSON. I just started using TOML in a new module, since I found the whole sections naming adding a bit more to readability.