By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,548 Members | 1,495 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,548 IT Pros & Developers. It's quick & easy.

Isn't there a better way?

P: n/a
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???

Jul 21 '06 #1
Share this Question
Share on Google+
15 Replies


P: n/a
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

Jul 21 '06 #2

P: n/a
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?

Jul 21 '06 #3

P: n/a
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

Jul 21 '06 #4

P: n/a
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???
Not sure if this is better, but you can use OptionParser to set attributes
on arbitrary objects, including your Processor instance:
>>class Processor(object):
.... def __init__(self):
.... self.verbose = False
.... self.index = 42
....
>>import optparse
parser = optparse.OptionParser()
parser.add_option("-v", "--verbose", action="store_true")
<Option at 0x40294aac: -v/--verbose>
>>parser.add_option("-i", "--index", type="int")
<Option at 0x40294a6c: -i/--index>
>>p = Processor()
parser.parse_args(args=["-i75", "-v"], values=p)
(<__main__.Processor object at 0x40298e0c>, [])
>>p.verbose, p.index
(True, 75)

Peter
Jul 21 '06 #5

P: n/a
T

T wrote:
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)"

Jul 21 '06 #6

P: n/a
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???
What's wrong with:

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

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 21 '06 #7

P: n/a
Peter Otten <__*******@web.dewrites:
[...]
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
Jul 21 '06 #8

P: n/a
In message <11**********************@m79g2000cwm.googlegroups .com>, 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???
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.
Jul 22 '06 #9

P: n/a
Lawrence D'Oliveiro a écrit :
In message <11**********************@m79g2000cwm.googlegroups .com>, 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???


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".
Jul 22 '06 #10

P: n/a
On 21 Jul 2006 07:51:15 -0700 in comp.lang.python, "T"
<ty*****@yahoo.comwrote:
>
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

--
Change is inevitable, progress is not.
Jul 22 '06 #11

P: n/a
In message <44***********************@news.free.fr>, Bruno Desthuilliers
wrote:
Lawrence D'Oliveiro a écrit :
>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".
I think I'm going to plonk you.
Jul 24 '06 #12

P: n/a
Lawrence D'Oliveiro wrote:
In message <44***********************@news.free.fr>, Bruno Desthuilliers
wrote:

>>Lawrence D'Oliveiro a écrit :

>>>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".


I think I'm going to plonk you.
And the rest of comp.lang.python has to know about this because ... ?

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

Jul 24 '06 #13

P: n/a
In message <ma***************************************@python. org>, Steve
Holden wrote:
Lawrence D'Oliveiro wrote:
>In message <44***********************@news.free.fr>, Bruno Desthuilliers
wrote:

>>>Lawrence D'Oliveiro a écrit :
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".


I think I'm going to plonk you.

And the rest of comp.lang.python has to know about this because ... ?
Some of them might be worth plonking as well?
Jul 24 '06 #14

P: n/a
Lawrence D'Oliveiro wrote:
In message <44***********************@news.free.fr>, Bruno Desthuilliers
wrote:

>>Lawrence D'Oliveiro a écrit :

>>>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".


I think I'm going to plonk you.
Do what you want.

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 24 '06 #15

P: n/a
Lawrence D'Oliveiro wrote:
In message <ma***************************************@python. org>, Steve
Holden wrote:

>>Lawrence D'Oliveiro wrote:
>>>In message <44***********************@news.free.fr>, Bruno Desthuilliers
wrote:

Lawrence D'Oliveiro a écrit :

>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".
I think I'm going to plonk you.

And the rest of comp.lang.python has to know about this because ... ?


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.

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 24 '06 #16

This discussion thread is closed

Replies have been disabled for this discussion.