How to pass arguments to python script similar to MATLAB function script?
<p>I have a python script that implements several functions, but I want to be flexible in terms of which functions I use every time. When I would run the Python Script I would want to pass some arguments that would stand as "flags" for executing my functions.</p>

<p>In MATLAB would look something like this:</p>

<pre><code>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
</code></pre>

<p>Which I call from command window with (for example):</p>

<pre><code>metrics(1,0)
</code></pre>

<p>In <strong>Python</strong> I tried using </p>

<blockquote>
  <p>def metrics(min, max)</p>
  
  <p>argparse()</p>
</blockquote>

<p>and</p>

<blockquote>
  <p>os.system("metrics.py 1,1")</p>
</blockquote>

<p>Any suggestion how to transpose the MATLAB function calling in the <strong>Python Shell</strong> (I am using Anaconda's Spyder for the matter)?</p>

<p>You can use the results directly just like you do in MATLAB.  The argument parsing stuff is for calling python scripts from the system command prompt or shell, not from the Python shell.  So have a script file like <code>myscript.py</code>:</p>

<pre><code>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')
</code></pre>

<p>Then, from the python or ipython shell, do:</p>

<pre><code>&gt;&gt;&gt; from myscript import metrics
&gt;&gt;&gt; metrics(1, 0)
</code></pre>

<p>Although typically in Python you would use <code>True</code> and <code>False</code>.  Also, I changed the argument names since they are too easy to confuse with builtins, and in Python you don't need the <code>== 1</code>.  Also, Python supports default arguments, so you could do something like this:</p>

<pre><code>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')
</code></pre>

<p>then:</p>

<pre><code>&gt;&gt;&gt; from myscript import metrics
&gt;&gt;&gt; matrics(True)
&gt;&gt;&gt; metrics(maxval=True)
</code></pre>

<p>Also, Python supports something called ternary expressions, which are basically <code>if...else</code> expressions.  So this:</p>

<pre><code>    if maxval:
        print(max(x))
    else:
        print('else')
</code></pre>

<p>Could be written as:</p>

<pre><code>    print(max(x) if maxval else 'else')
</code></pre>

python matlab function