Does Python have equivalent to MATLAB "varargin", "varargout", "nargin", "nargout"?

Discussion in 'Python' started by openopt@ukr.net, Feb 18, 2007.

  1. Guest

    Thank you in advance for your response.
    Dmitrey
     
    , Feb 18, 2007
    #1
    1. Advertising

  2. Jorge Godoy Guest

    writes:

    > Thank you in advance for your response.


    And those do ... ?

    --
    Jorge Godoy <>
     
    Jorge Godoy, Feb 18, 2007
    #2
    1. Advertising

  3. Re: Does Python have equivalent to MATLAB "varargin", "varargout","nargin", "nargout"?

    Where you would use varargin and nargin in Matlab, you would use the
    *args mechanism in Python.

    Try calling

    def t1(*args):
    print args
    print len(args)

    with different argument lists

    Where you would use varargout and nargout in Matlab you would use tuple
    unpacking in Python.

    Play with this

    def t2(n):
    return tuple(range(n))

    a, b = t2(2)

    x = t2(3)
     
    Andrew McLean, Feb 18, 2007
    #3
  4. Dan Bishop Guest

    Re: Does Python have equivalent to MATLAB "varargin", "varargout", "nargin", "nargout"?

    On Feb 18, 12:58 pm, wrote:
    > Thank you in advance for your response.
    > Dmitrey


    The Python equivalent to "varargin" is "*args".

    def printf(format, *args):
    sys.stdout.write(format % args)

    There's no direct equivalent to "varargout". In Python, functions
    only have one return value. However, you can simulate the effect of
    multiple return values by returning a tuple.

    "nargin" has no direct equivalent either, but you can define a
    function with optional arguments by giving them default values. For
    example, the MATLAB function

    function [x0, y0] = myplot(x, y, npts, angle, subdiv)
    % MYPLOT Plot a function.
    % MYPLOT(x, y, npts, angle, subdiv)
    % The first two input arguments are
    % required; the other three have default values.
    ...
    if nargin < 5, subdiv = 20; end
    if nargin < 4, angle = 10; end
    if nargin < 3, npts = 25; end
    ...

    would be written in Python as:

    def myplot(x, y, npts=25, angle=10, subdiv=20):
    ...
     
    Dan Bishop, Feb 18, 2007
    #4
  5. On 18 Feb 2007 10:58:07 -0800, declaimed the following
    in comp.lang.python:

    > Thank you in advance for your response.
    > Dmitrey


    Something more than?

    Microsoft Windows XP [Version 5.1.2600]
    (C) Copyright 1985-2001 Microsoft Corp.

    C:\Documents and Settings\Dennis Lee Bieber>python
    ActivePython 2.4.3 Build 12 (ActiveState Software Inc.) based on
    Python 2.4.3 (#69, Apr 11 2006, 15:32:42) [MSC v.1310 32 bit (Intel)] on
    win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> def args(*arg):

    .... print "Length: ", len(arg)
    .... for i, j in enumerate(arg):
    .... print "Argument %s: %s" % (i, j)
    .... retlen = int(len(arg) / 2 + 0.5)
    .... return arg[:retlen]
    ....
    >>> print "return: %s" % args(1, "a", 3)

    Length: 3
    Argument 0: 1
    Argument 1: a
    Argument 2: 3
    return: 1
    >>> print "return: %s" % (args(1, "a", 3, 3.1415926),)

    Length: 4
    Argument 0: 1
    Argument 1: a
    Argument 2: 3
    Argument 3: 3.1415926
    return: (1, 'a')
    >>> print "return: %s" % (args(1, "a", 3, 3.1415926, "Hello World", "how many"),)

    Length: 6
    Argument 0: 1
    Argument 1: a
    Argument 2: 3
    Argument 3: 3.1415926
    Argument 4: Hello World
    Argument 5: how many
    return: (1, 'a', 3)
    >>>

















    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Feb 18, 2007
    #5
  6. Guest

    Re: Does Python have equivalent to MATLAB "varargin", "varargout", "nargin", "nargout"?

    Ok, thx
    But can I somehow determing how many outputs does caller func require?
    for example:
    MATLAB:
    function [objFunVal firstDerive secondDerive] = simpleObjFun(x)
    objFunVal = x^3;
    if nargout>1
    firstDerive = 3*x^2;
    end
    if nargout>2
    secondDerive = 6*x;
    end

    So if caller wants only
    [objFunVal firstDerive] = simpleObjFun(15)
    than 2nd derivatives don't must to be calculated with wasting cputime.
    Is something like that in Python?
     
    , Feb 19, 2007
    #6
  7. Peter Otten Guest

    Re: Does Python have equivalent to MATLAB "varargin", "varargout", "nargin", "nargout"?

    wrote:

    > Ok, thx
    > But can I somehow determing how many outputs does caller func require?
    > for example:
    > MATLAB:
    > function [objFunVal firstDerive secondDerive] = simpleObjFun(x)
    > objFunVal = x^3;
    > if nargout>1
    > firstDerive = 3*x^2;
    > end
    > if nargout>2
    > secondDerive = 6*x;
    > end
    >
    > So if caller wants only
    > [objFunVal firstDerive] = simpleObjFun(15)
    > than 2nd derivatives don't must to be calculated with wasting cputime.
    > Is something like that in Python?


    For a sequence whose items are to be calulated on demand you would typically
    use a generator in Python. You still have to provide the number of items
    somehow (see the head() helper function).

    from itertools import islice

    def derive(f, x0, eps=1e-5):
    while 1:
    yield f(x0)
    def f(x, f=f):
    return (f(x+eps) - f(x)) / eps

    def head(items, n):
    return tuple(islice(items, n))

    if __name__ == "__main__":
    def f(x): return x**6
    print head(derive(f, 1), 4)

    Peter
     
    Peter Otten, Feb 19, 2007
    #7
  8. Robert Kern Guest

    Re: Does Python have equivalent to MATLAB "varargin", "varargout","nargin", "nargout"?

    wrote:
    > Ok, thx
    > But can I somehow determing how many outputs does caller func require?
    > for example:
    > MATLAB:
    > function [objFunVal firstDerive secondDerive] = simpleObjFun(x)
    > objFunVal = x^3;
    > if nargout>1
    > firstDerive = 3*x^2;
    > end
    > if nargout>2
    > secondDerive = 6*x;
    > end
    >
    > So if caller wants only
    > [objFunVal firstDerive] = simpleObjFun(15)
    > than 2nd derivatives don't must to be calculated with wasting cputime.
    > Is something like that in Python?


    Return an object with each of the results objFunVal, firstDerive, and
    secondDerive as attributes (or a dictionary). Use keyword arguments to inform
    the function of which ancillary computations it needs to perform.

    If at all possible, don't change the number of return values. It's annoying to
    deal with such an API.

    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco
     
    Robert Kern, Feb 19, 2007
    #8
  9. juh

    Joined:
    Nov 17, 2011
    Messages:
    1
    use of key arguments

    For input arguments, consider using keyword arguments,
    very useful in python :

    def f(a, b=42, **kargs):
    print a*b, 'and', kargs​

    The constrain is that, appart from a and b, keyword must be used when
    passing arguments. However, this feature is really nice and I recommend
    to use it as much as possible.
     
    Last edited: Nov 17, 2011
    juh, Nov 17, 2011
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. siki
    Replies:
    0
    Views:
    989
  2. Stef Mientki
    Replies:
    1
    Views:
    595
    Carl Banks
    Dec 30, 2006
  3. Replies:
    4
    Views:
    444
    milhous
    Sep 22, 2006
  4. dmitrey
    Replies:
    3
    Views:
    366
    sturlamolden
    Mar 15, 2007
  5. Luna Moon
    Replies:
    16
    Views:
    1,402
    Giovanni Gherdovich
    Aug 8, 2008
Loading...

Share This Page