Isn't there a better way?

Steve Holden steve at holdenweb.com
Fri Jul 21 11:50:25 EDT 2006


T wrote:
> I am using an optparse to get command line options, and then pass them
> to an instance of another class:
> 
> 
> 
> # Class that uses optparse.OptionParser
> foo = Parse_Option()
> 
> # Class that does the real work
> bar = Processor()
> 
> bar.index = foo.options.index
> bar.output  = foo.options.output
> bar.run()
> 
> 
> 
> This works, but it feels hokey or unnatural to "pass" data from one
> class to another.  Isn't there a better way???
> 
The standard way would be to have the Processor class's __init__() 
method take two arguments which it would then assign to the appropriate 
instance attributes. You could, of course, provide default values if you 
felt so inclined.

Then you could just use

bar = Processor(foo.options.index, foo.options.output)

or something similar. That's better anyway, as it seems to make the 
coupling more explicit. What would you do if the index and output values 
were just stored in plain variables?

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb       http://holdenweb.blogspot.com
Recent Ramblings     http://del.icio.us/steve.holden




More information about the Python-list mailing list