为什么可以在*之后使用函数中的内容导入包*? [英] Why can you import a package *after* using its content in a function?

查看:118
本文介绍了为什么可以在*之后使用函数中的内容导入包*?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用MATLAB R2014b并且有一个问题,我将通过以下示例说明。

I'm on MATLAB R2014b and have a question that I will illustrate with the following example.

MWE可以如下制作或将其下载为.zip文件

MWE can be made as follows or download it as a .zip file here.

在您的路径上创建一个包文件夹 + test ,其中包含四个函数文件:

Create a package folder +test on your path with four function files in it:

+test
    a.m
    b.m
    c.m
    d.m

内容上午

function a
disp 'Hello World!'

内容 bm

function b
a

如果从命令行运行 b ,则必须导入测试首先打包(导入测试。* )或运行 test.b

If you run b from the command line, you will have to import the test package first (import test.*) or run test.b.

运行 b 将导致错误,因为函数范围 b 不包含函数 a 。我们必须先导入它才能使用它。为此,我创建了 cm

Running b will result in an error, since the scope of function b doesn't contain function a. We must import it before it can be used. For this I've created c.m:

function c
import test.*
a

现在正在运行 c 工作正常。

Now running c works fine.

现在我的问题。如果我将 cm 更改为(保存在 dm ):

Now my question. If I change c.m to (saved in d.m):

function d
a
import test.*



,p 调用包函数 a 后,发出import命令。运行 d 仍然可以正常工作,就好像 d.m 中导入命令的位置无关紧要。导入似乎是在调用函数 a 之前发生的,在导入之前的行中发生了 dm

I.e. the import command is issued after the call to package function a. Running d still works just fine, as if the position of the import command in d.m does not matter. The import appears to have occurred before the call to function a, which in d.m happens on the line before the import.

为什么会发生这种情况。这是预期的行为,它的用途是什么? MATLAB如何以及以什么顺序读取 .m 文件并进行处理?更偏离主题,但总的来说:与MATLAB相比,如何导入用不同语言处理的包,命令的顺序是否重要?

Why does this happen. Is this the intended behaviour and what are its uses? How and in what order does MATLAB read a .m file and process it? And more off-topic, but in general: how is importing packages handled in different languages compared to MATLAB, does the order of commands matter?

我的先发制人的结论基于注释:最好只使用MATLAB代码开头或附近的导入函数。这清楚地显示了导入的内容在整个元素(例如函数)中可用。它还可以防止错误的假设,即在导入之前,内容尚未可用或引用具有相同名称的其他内容。

My preemptive conclusion based on the comments: It is probably best practice to only use the import function at or near the beginning of MATLAB code. This makes clearly visible the imported content is available throughout the entire element (e.g. function). It also prevents the incorrect assumption that before the import, the content is not yet available or refers to a different thing with the same name.

推荐答案

MATLAB在评估函数之前执行静态代码分析,以确定该函数使用的变量/函数。评估 import 语句是此静态代码分析的一部分。这是设计原因,因为如果您 import 一个包然后使用它的函数,MATLAB需要在静态代码分析期间知道这一点。因此,无论 where 你在函数中放置 import 语句,它都会产生与它在开头时相同的效果。函数。

MATLAB performs static code analysis prior to evaluating a function in order to determine the variables/functions used by that function. Evaluation of the import statements is part of this static code analysis. This is by design because if you import a package and then use it's functions, MATLAB needs to know this during the static code analysis. As a result, regardless of where you put the import statement within your function, it will have the same effect as if it were at the beginning of the function.

您可以通过查看 import 输出来轻松测试这将列出所有当前导入的包。

You can easily test this by looking at the output of import which will list all of the current imported packages.

+ test / am

+test/a.m

function a(x)
    disp(import)
    import test.*
end

test.a()

%   test.*

这就是文档说明 不<的原因/ em>在条件中放入 import 语句。


不要使用 import 在函数内的条件语句中。在评估条件语句中的变量之前,MATLAB预处理 import 语句。

Do not use import in conditional statements inside a function. MATLAB preprocesses the import statement before evaluating the variables in the conditional statements.



function a(x)
    disp(import)
    if x
        import test.*
    else
        import othertest.*
    end
end

test.a()

%   test.*
%   othertest.*

避免此行为的唯一方法是允许静态代码分析器确定(毫无疑问) import 语句将不会被执行。我们可以通过使条件语句只是一个逻辑值来实现这一点。

The only way to avoid this behavior is to allow the static code analyzer to determine (without a doubt) that an import statement won't be executed. We can do this by having our conditional statement be simply a logical value.

function a()
    disp(import)
    if true
        import test.*
    else
        import othertest.*
    end
end

test.a()

%   test.*

至于导入与其他语言相比,这真的取决于语言。例如,在Python中,您必须在访问模块内容之前放置 import 。根据我的经验,这是典型案例,但我确信有很多例外。每种语言都会有所不同。

As far as importing compared to other languages, it really depends on the language. In Python for example, you must place the import before accessing the module contents. In my experience, this is the typical case but I'm sure there are many exceptions. Every language is going to be different.

这篇关于为什么可以在*之后使用函数中的内容导入包*?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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