有没有更好的方法? [英] Isn't there a better way?

查看:79
本文介绍了有没有更好的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我使用optparse来获取命令行选项,然后将它们传递给另一个类的实例:


#使用optparse.OptionParser的类

foo = Parse_Option()


#完成实际工作的班级

bar =处理器()


bar.index = foo.options.index

bar.output = foo.options.output

bar.run()


这样可行,但通过感觉很不自然或不自然从一个

类到另一个类的数据。难道不是更好的方式吗?


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

推荐答案

Le Fri,2006年7月21日07:51:15 -0700, Taécrit*:
Le Fri, 21 Jul 2006 07:51:15 -0700, T a écrit*:

>

我使用optparse获取命令行选项,然后传递它们

到另一个类的实例:


#使用optparse.OptionParser的类

foo = Parse_Option()

#完成实际工作的班级

bar =处理器()


bar.index = foo.options.index

bar.output = foo.options.output

bar.run()


这样可行,但感觉很糟糕或不自然通过"从一个

类到另一个类的数据。难道不是更好的方法吗?
>
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???



我不知道这两个课程的作用,但这可以解决吗?

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

self.output = self.option.output


def run(self):< br $>
#code


bar.run()

Rony

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

我不知道两个班级做什么[剪断]


foo基本上获得了详细(真或假)和索引的值(

整数)来自命令行选项,使用

optparse.OptionParser.add_option()和

optparse.OptionParser.parse_args()


我想要bar访问verbose和index的值。还有另外一种方式吗?

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?


T写道:
T wrote:

我使用optparse获取命令行选项,然后将它们传递给另一个类的实例:


#使用optparse.OptionParser的类

foo = Parse_Option()


#完成实际工作的班级

bar =处理器()


bar.index = foo.options.index

bar.output = foo.options.output

bar.run()


这样可行,但通过感觉很不自然或不自然从一个

类到另一个类的数据。难道没有更好的方法吗?
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???



标准方法是让处理器类的__init __()

方法接受两个参数,然后将它们分配给相应的

实例属性。当然,如果你感觉如此倾向,你可以提供默认值。


然后你可以使用


bar =处理器(foo.options.index,foo.options.output)


或类似的东西。无论如何,这似乎更好,因为它似乎使

耦合更明确。如果索引和输出值

只存储在普通变量中,你会怎么做?


问候

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

最近的Ramblings http://del.icio.us/steve.holden

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


这篇关于有没有更好的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆