While the method of modifying your source file mentioned by others would probably work, I would highly recommend looking into other solutions such as storing a configuration file with the relevant information that is changing. If you don't want to write this functionality yourself (which can be as simple as a hidden file in the same directory as the script with a single line containing, say, an api key) you can look into solutions such as ConfigParser, using JSON, or even just pickling an object. When you need to access that information, such as the value of an API key, you can read in the configuration file.
In general, if you need to reference something by a value look into using dictionaries. For instance, if my api key was "abc", instead of accessing foo.abc
, I could put a dictionary d = {"abc": value}
on foo
and access foo.d["abc"]
Again, as mentioned by others, it is generally considered bad practice to write self-modifying code since it can be very hard to read and maintain. If you have the ability to write to the directory with the code file in it, there are rarely good reasons to modify that file over using a configuration/persistent state file.