English 中文(简体)
甲型设计模式,进口
原标题:Python design patterns, cross importing
  • 时间:2009-08-26 11:43:00
  •  标签:

I am using Python for automating a complex procedure that has few options. I want to have the following structure in python. - One "flow-class" containing the flow - One helper class that contains a lot of "black boxes" (functions that do not often get changed).

99%的时间里,我修改了流层中的物品,因此我只想制定时常加以修改的代码,这样我就不必在一片土地上开车,以找到我想修改的法典。 这一类别还包含经常发生变化的全球变量(组合环境)。 助手类别包含全球变量,但往往没有变化。

In the flow-class I have a global variable that I want the user to be forced to input at every run. The line looks like this. print ("Would you like to see debug output (enter = no)? ") debug = getUserInput()

自动交换机功能应当位于助手类别,因为它从未修改过。 获取用户数据需要流量类别产生的全球变量,这表明用户投入是否应当与六氯环己烷的指挥线或Eclipse(在Windows上运行)。

My question is: How can I structure this in the best way? Currently it looks like the following: The flow-class:

import helper_class

isLinux = 1

debug = getUserInput()

助手:

import os, flow_class

def getUserInput():
    userInput = input ()
    if (flow_class.isLinux == 1):
        userInput = userInput[:-1]
    return userInput

这给我带来了以下错误:

Traceback (most recent call last):
  File "flow_class.py", line 1, in <module>
    import helper_class
  File "helper_class.py", line 1, in <module>
    import os, flow_class
  File "flow_class.py", line 5, in <module>
    debug = getUserInput()
NameError: name  getUserInput  is not defined

我知道,我显然可以通过总是把“六氯环己烷”作为获取UserInput的一个参数来解决这一问题,但这使这一方法的使用复杂化,使之不那么直观。

最佳回答

我知道,我显然可以通过总是通过<条码>->-六氯环己烷/代码”作为获取UserInput的参数来解决这一问题,但这使这一方法的使用复杂化,使之不那么直觉。

实际使用全球变量使这一方案的使用复杂化,而瓦阿奈则大于简单的参数。

尝试:

debug = getUserInput(isLinux=True)

这里还有一些建议

  • You mention there are lots of parameters that you ll change often. Should these be hard coded? Try using a configuration file, or passing a dict() from flow as a parameter. That way you have a central place to change common variables without having to dive in!
  • your flow/helper class sounds like a Controller/Model paradigm. This is good. But your model shouldn t have to import your controller.

这些是一般方案拟定做法,具体针对的是onic体。 如果你对方案设计的尝试读到, 它们在工作流程和设计方面有很大的局限性。 还有Code Full,Roberto建议。

问题回答

页: 1 它并不涉及交叉进口。 一旦确定出你的话,就会有<代码>AttributeError,这确实与交叉进口有关。

在本阶段,您必须执行在进口<代码>的>/之前界定的“接通”的逻辑。

对您的最后发言进行评论:你的假设是不正确的。 如果你使用明确的当地价值观,法典将更加清楚。

我要对您的最后一句提出质疑。

Usually, as also outlined in CC2, usage of global variables helps in writing code, but not in reading it.
And code is read many more times than it is written; in your case, I understand you modify the same script over and over again.

The problem you are facing now is just a consequence of the generic design decision to use global variables.
Explicit parameter passing would make it much clearer, and easier to maintain.

如Zentarzak所说,explicit优于默示





相关问题
热门标签