How to store all flags in dict to pass it to other class


so far I have a guild config file, a train file and a trainer class.

# guild config file
  main: train
      default: 2
      default: 3
# file
import Trainer
trainer = Trainer(configs)
# Trainer class
class Trainer:
  __init__(self, config):
  self.x = config.x

How can I store all flags in a dictionary and pass it to the constructor of the trainer class without listing all flags again?

I know I could do something like (not what I want):

# file
import Trainer

trainer = Trainer({"x": x, "y": y})

Here’s a gist that shows a couple of approaches.

In the case of train, it does what you’re looking to do. It’s a bit dangerous though to update an object namespace using an uncontrolled dict like that.

The train2 op is an alternative that uses Python’s SimpleNamespace to provide config vals. In that case, the trainer object access self.config rather than the values directly (e.g. self.x, self.y). I like this approach better as it ensures that config can’t accidentally corrupt your object. That may not work for you though. I provide just as example.

1 Like

Great answer thank you! I missed the global:params feature in the documentation.

1 Like