简体   繁体   English

如何在另一个过程中调用一个过程

[英]How to call a procedure inside of another procedure

I'm working on creating a large .py file that can be imported and used to solve mathematical formulas. 我正在创建一个大的.py文件,该文件可以导入并用于解决数学公式。 I'd like to store the formulas in a procedure that is called input1_input2_input3(): for example the formual distance=speed*time is called dis_spe_tim(). 我想将公式存储在一个名为input1_input2_input3()的过程中:例如,正式的distance = speed * time称为dis_spe_tim()。

The code so far is: 到目前为止的代码是:

def dis_spe_tim():
    def distance(speed, time):
        result = speed*time
        unit = input("What unit are you measuring the distance in?")
        print(resule,unit)

    def speed():
        print("speed")

and I would ideally like the user to use this like so: 我理想上希望用户像这样使用它:

import equations #name of the .py file
from equations import *
dis_spe_tim.distance(1,2)

Unfortunately, this is my first time ever doing something like this so I have absolutely no idea how to go about calling the procedure inside of the procedure and providing its arguments. 不幸的是,这是我第一次做这样的事情,因此我完全不知道如何在过程内部调用过程并提供其参数。

Short answer: you can't. 简短的答案:您不能。 Nested functions are local to the function they're defined in and only exists during the outer function's execution ( def is an executable statement that, at runtime, creates a function object and bind it to it's name in the enclosing namespace). 嵌套函数对于定义它们的函数来说是本地的,并且仅在外部函数执行期间存在( def是一个可执行语句,在运行时会创建一个函数对象并将其绑定到其在命名空间中的名称)。

The canonical python solution is to use modules as namespaces (well, Python modules ARE, mainly, namespaces), ie have a distinct module for each "formula", and define the functions at the module's top-level: 规范的python解决方案是将模块用作名称空间(嗯,Python模块主要是名称空间),即每个“公式”都有一个不同的模块,并在模块的顶层定义功能:

# dis_spe_tim.py
def distance(speed, time):
   # code here

def speed():
   # code here

Then put all those modules in an equations package (mostly: a folder containing modules and an __init__.py file). 然后将所有这些模块放入equations程序包(通常是:包含模块的文件夹和__init__.py文件)。 Then you can do: 然后,您可以执行以下操作:

from equations import dis_spe_tim
dis_spe_tim.distance(1,2)

You can check the doc for more on modules and packages here: https://docs.python.org/3/tutorial/modules.html#packages 您可以在以下位置查看文档以获取有关模块和软件包的更多信息: https : //docs.python.org/3/tutorial/modules.html#packages

Also note that 另请注意

1/ "star imports" (also named "wildcard imports"), ie from somemodule import * , are highly discouraged as they tend to make the code harder to read and maintain and can cause unexpected (and sometimes subtles enough to be hard to spot) breakages. 1 / 强烈建议不要使用 “星号导入”(也称为“通配符导入”),即from somemodule import * ,因为它们会使代码更难以阅读和维护,并可能导致意外情况(有时有些微妙,难以发现) )损坏。

2/ you shouldn't mix "domain" code (code that do effective computations) with UI code (code that communicates with the user), so any call to input() , print() etc should be outside the "domain" code. 2 /您不应将“域”代码(进行有效计算的代码)与UI代码(与用户进行通信的代码)混合使用,因此对input()print()等的任何调用都应在“域”代码之外。 This is key to make your domain code usable with different UIs (command-line, text-based (curse etc), GUI, web, whatever), but also, quite simply, to make sure your domain code is easily testable in isolation (unit testing...). 这是使您的域代码可用于不同的UI(命令行,基于文本(诅咒等),GUI,Web等)的关键,而且非常简单地确保您的域代码可轻松地进行隔离测试(单元测试...)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM