Isn't there a better way?

T

T

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???
 
T

T

I don't know what both classes do [snip]

foo basically gets values for verbose (a True or False) and index (an
integer) from command line options, using
optparse.OptionParser.add_option() and
optparse.OptionParser.parse_args()

I would like bar to access the values of verbose and index. Is there
another way?
 
S

Steve Holden

T said:
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
 
P

Peter Otten

T said:
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???

Not sure if this is better, but you can use OptionParser to set attributes
.... def __init__(self):
.... self.verbose = False
.... self.index = 42
....
import optparse
parser = optparse.OptionParser()
parser.add_option("-v", "--verbose", action="store_true")
parser.add_option("-i", "--index", type="int")
p = Processor()
parser.parse_args(args=["-i75", "-v"], values=p)
p.verbose, p.index
(True, 75)

Peter
 
R

rony steelandt

Le Fri, 21 Jul 2006 07:51:15 -0700, T a écrit :
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???

I don't know what both classes do, but can this be a solution ?


class Processor(Parse_Option)
self.index = self.option.index
self.output = self.option.output

def run(self):
# code

bar.run()


Rony
 
T

T

T said:
I don't know what both classes do [snip]

foo basically gets values for verbose (a True or False) and index (an
integer) from command line options, using
optparse.OptionParser.add_option() and
optparse.OptionParser.parse_args()

I would like bar to access the values of verbose and index. Is there
another way?

Sorry... I meant to say:

"foo basically gets values for index (an integer) and output (a string)"
 
B

Bruno Desthuilliers

T said:
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???

What's wrong with:

foo = Parse_Option()
bar = Processor()
bar.run(foo.options.index, foo.options.output)
 
J

John J. Lee

Peter Otten said:
Not sure if this is better, but you can use OptionParser to set attributes
on arbitrary objects, including your Processor instance:
[...]

I'd guess it's not worth the mental overhead required when you
discover the next argument no longer fits this scheme. Or the mess
when you start lying to yourself about what the interface of Processor
should be, just so you can implement it like this.

Still, better than just passing 'options' (from options, args =
parser.parse_args()) to all your functions / classes, which... well,
only makes sense if you get paid by the hour, and think that way, too
;-)


John
 
L

Lawrence D'Oliveiro

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???

I don't see the problem. If you're calling a number of different routines in
the Processor class, all accessing the same data, then it makes perfect
sense to only pass it once.
 
B

Bruno Desthuilliers

Lawrence D'Oliveiro a écrit :
I don't see the problem.

The problem is setting bar attributes from the outside IMHO. Which is
easily solved by passing the relevant stuff either at instanciation time
or at call time.
If you're calling a number of different routines in
the Processor class, all accessing the same data, then it makes perfect
sense to only pass it once.

Actually they are not "passed".
 
D

Dave Hansen

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()


How about

class Processor(whatever):
...
def __init__(self,whatever_else):
...
self.op = Parse_Option()

...
bar = Processor()
bar.run()

This would even let you do some preliminary option processing during
object initialization.

Regards,


-=Dave
 
B

Bruno Desthuilliers

Lawrence said:
In message <[email protected]>, Steve
Holden wrote:




Some of them might be worth plonking as well?

Certainly. I suggest that you put in your killfile anyone not agreeing
with you on any point, or having the bad idea to point out inexact or
ambigous stuff in your posts. This will surely make this ng a very more
friendly place to you - while perhaps not as useful.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top