如何避免过多的参数传递? [英] How to avoid excessive parameter passing?

查看:100
本文介绍了如何避免过多的参数传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个中等大小的程序在python分布在5个模块。该程序使用主模块中的OptionParser来接受命令行参数,例如main.py.这些选项随后用于确定其他模块中的方法如何运行(例如,a.py,b.py)。当我扩展用户自定义行为或程序的能力时,我发现我最终要求在an.py中的方法中使用这个用户定义的参数,而不是由main.py直接调用,而是由另一个方法in.py:



main.py:

  import a 
p = some_command_line_argument_value
a.meth1(p)

a.py:

  meth1(p):
#一些代码
res = meth2(p)
#一些更多的代码w / res

meth2(p):
#做一些与p

这个过多的参数传递似乎是浪费和错误的,但是很难,因为我尝试我不能想到一个解决这个问题的设计模式。虽然我已经进行了一些正式的CS教育(在CS中的CS中是次要的),但是自从我开始使用python以来,我才真正感受到了很好的编码习惯。请帮助我成为一个更好的程序员!

解决方案

也许你应该将你的代码更多地组织成类和对象?正如我写的那样,Jimmy展示了一个基于类实例的答案,所以这里是一个纯粹的基于类的答案。这将是最有用的,如果你只想要一个单一的行为;如果有任何机会,您可能会希望在某些时候使用不同的默认值,那么您应该在Python中使用普通的面向对象编程,即在实例中传递具有属性p set的类实例,而不是类。

  class Aclass(object):
p = None
@classmethod
def init_p(cls,value):
p = value
@classmethod
def meth1(cls):
#some code
res = cls.meth2()
#some more code w / res
@classmethod
def meth2(cls):
#从p
通过

从导入Aclass作为ac

ac.init_p(some_command_line_argument_value)

ac.meth1()
ac.meth2()


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 options are later used to determine how methods in other modules behave (e.g. a.py, b.py). As I extend the ability for the user to customise the behaviour or the program I find that I end up requiring this user-defined parameter in a method in a.py that is not directly called by main.py, but is instead called by another method in a.py:

main.py:

 import a
 p = some_command_line_argument_value
 a.meth1(p)

a.py:

meth1(p):
       # some code
       res = meth2(p)
       # some more code w/ res

meth2(p):
       # do something with p

This excessive parameter passing seems wasteful and wrong, but has hard as I try I cannot think of a design pattern that solves this problem. While I had some formal CS education (minor in CS during my B.Sc.), I've only really come to appreciate good coding practices since I started using python. Please help me become a better programmer!

解决方案

Maybe you should organize your code more into classes and objects? As I was writing this, Jimmy showed a class-instance based answer, so here is a pure class-based answer. This would be most useful if you only ever wanted a single behavior; if there is any chance at all you might want different defaults some of the time, you should use ordinary object-oriented programming in Python, i.e. pass around class instances with the property p set in the instance, not the class.

class Aclass(object):
    p = None
    @classmethod
    def init_p(cls, value):
        p = value
    @classmethod
    def meth1(cls):
        # some code
        res = cls.meth2()
        # some more code w/ res
    @classmethod
    def meth2(cls):
        # do something with p
        pass

from a import Aclass as ac

ac.init_p(some_command_line_argument_value)

ac.meth1()
ac.meth2()

这篇关于如何避免过多的参数传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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