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

21

u/[deleted] Apr 25 '21

[removed] — view removed comment

7

u/Concretesurfer18 Apr 25 '21

Can a config.py update a setting within it that was changed while the program is running like you can with a json?

1

u/vectorpropio Apr 25 '21

Can you expand a little? I'm relatively new in python and not so good in English to grasp what you say.

You are talking about reloading the configuration file? Saving changes to the configuration file? Changing the settings on the fly overriding the configuration file? Or something different?

2

u/Concretesurfer18 Apr 25 '21

After loading the config I have often just written changes to the config using Json. This would be saving changes to the file and overriding the previous config file at the same time.

→ More replies (4)