How to avoid excessive parameter passing?

后端 未结 6 961
予麋鹿
予麋鹿 2021-02-11 00:22

I am developing a medium size program in python spread across 5 modules. The program accepts command line arguments using OptionParser in the main module e.g. main.py. These opt

6条回答
  •  鱼传尺愫
    2021-02-11 01:02

    Create objects of types relevant to your program, and store the command line options relevant to each in them. Example:

    import WidgetFrobnosticator
    f = WidgetFrobnosticator()
    f.allow_oncave_widgets = option_allow_concave_widgets
    f.respect_weasel_pins = option_respect_weasel_pins
    
    # Now the methods of WidgetFrobnosticator have access to your command-line parameters,
    # in a way that's not dependent on the input format.
    
    import PlatypusFactory
    p = PlatypusFactory()
    p.allow_parthenogenesis = option_allow_parthenogenesis
    p.max_population = option_max_population
    
    # The platypus factory knows about its own options, but not those of the WidgetFrobnosticator
    # or vice versa.  This makes each class easier to read and implement.
    

提交回复
热议问题