简体   繁体   English

如何将参数传递给类似于MATLAB函数脚本的python脚本?

[英]How to pass arguments to python script similar to MATLAB function script?

I have a python script that implements several functions, but I want to be flexible in terms of which functions I use every time. 我有一个实现多个功能的python脚本,但我想在每次使用的功能方面保持灵活性。 When I would run the Python Script I would want to pass some arguments that would stand as "flags" for executing my functions. 当我运行Python脚本时,我想传递一些参数,这些参数将作为执行功能的“标志”。

In MATLAB would look something like this: 在MATLAB中将如下所示:

function metrics( Min, Max )
%UNTITLED Summary of this function goes here
%   Detailed explanation goes here
x = [1,2,3,4,5,5];

if (Min==1)
    min(x)
else
    disp('something')
end

if (Max==1)
    max(x)
else
    disp('else')
end

end

Which I call from command window with (for example): 我从命令窗口调用的(例如):

metrics(1,0)

In Python I tried using Python中,我尝试使用

def metrics(min, max) def指标(最小,最大)

argparse() argparse()

and

os.system("metrics.py 1,1") os.system(“ metrics.py 1,1”)

Any suggestion how to transpose the MATLAB function calling in the Python Shell (I am using Anaconda's Spyder for the matter)? 有什么建议如何转换在Python Shell中调用的MATLAB函数(我正在使用Anaconda的Spyder)?

You can use the results directly just like you do in MATLAB. 您可以像在MATLAB中一样直接使用结果。 The argument parsing stuff is for calling python scripts from the system command prompt or shell, not from the Python shell. 参数解析的东西是用于从系统命令提示符或外壳程序而不是从Python Shell调用python脚本。 So have a script file like myscript.py : 所以有一个脚本文件,例如myscript.py

def metrics(minval, maxval):
    """UNTITLED Summary of this function goes here

    Detailed explanation goes here.
    """
    x = [1, 2, 3, 4, 5, 5]

    if minval:
        print(min(x))
    else:
        print('something')

    if maxval:
        print(max(x))
    else:
        print('else')

Then, from the python or ipython shell, do: 然后,从python或ipython shell中执行以下操作:

>>> from myscript import metrics
>>> metrics(1, 0)

Although typically in Python you would use True and False . 尽管通常在Python中,您将使用TrueFalse Also, I changed the argument names since they are too easy to confuse with builtins, and in Python you don't need the == 1 . 另外,我更改了参数名称,因为它们很容易与内建函数混淆,在Python中,您不需要== 1 Also, Python supports default arguments, so you could do something like this: 另外,Python支持默认参数,因此您可以执行以下操作:

def metrics(minval=False, maxval=False):
    """UNTITLED Summary of this function goes here

    Detailed explanation goes here.
    """
    x = [1, 2, 3, 4, 5, 5]

    if minval:
        print(min(x))
    else:
        print('something')

    if maxval:
        print(max(x))
    else:
        print('else')

then: 然后:

>>> from myscript import metrics
>>> matrics(True)
>>> metrics(maxval=True)

Also, Python supports something called ternary expressions, which are basically if...else expressions. 而且,Python支持称为三元表达式的东西,基本上就是if...else表达式。 So this: 所以这:

    if maxval:
        print(max(x))
    else:
        print('else')

Could be written as: 可以写成:

    print(max(x) if maxval else 'else')

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

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