I've been using the vanilla
def main():
# Do stuff
if __name__ == '__main__':
main()
but recently saw people doing
from absl import app
def main(_):
# Do things
if __name__ == '__main__':
app.run(main)
Abseil provides flags.FLAGS
, but I've been using ArgumentParser
, which works perfectly fine, so there is no win for Abseil in this aspect.
Then, why bother go the Abseil route?
PS: Related discussion on Reddit (which doesn't really answer this question): https://www.reddit.com/r/Python/comments/euhl81/is_using_googles_abseil_library_worth_the/
Consider a design pattern, where you are passing a json file (that contains say site-specific constants) at the cmd line as input to your Python script. Say,the json file contains immutable constants and you want to maintain it that way.
You want the constants from json file contents to be made available to all the modules within your project.
One way to do this is by implementing a central module that deserializes the json into a Python object. ABSL helps you to solve this by accessing (via the
FLAGS
) the input file in the central module and then storing it into a class variable so all modules across your project can use this.Without ABSL, you would need to first argparse the input file in main module, then send it to the central module.
A code example of this can be something like:
main.py
:centralmod.py
:and
someothermod.py
: