Main Content

Understand Python Function Arguments

Your Python® documentation shows you how to call a Python function. Python function signatures look similar to MATLAB® function signatures. However, Python has syntax which might be unfamiliar to MATLAB users.

Positional Arguments

A positional argument is passed by position. These arguments appear at the beginning of a function signature.

Python SignatureMATLAB Usage

abs(X)
Argument X is required.

>> py.abs(-99)

Some functions accept an arbitrary sequence of positional arguments, including no arguments. In Python, these arguments are defined by prepending the name with the * character.

Python SignatureMATLAB Usage

itertools.zip_longest(*iterables)
The iterables argument is not required, in which case, the function returns a zero length iterator.

Aggregate elements from two lists.
>> py.itertools.zip_longest(... py.list({1:10}),py.list({'a','b'}));


Create zero length iterator.
>> py.itertools.zip_longest;

print(*objects)

>> words = {'Hello','World!'};
>> py.print(words{:})

Keyword Arguments

A keyword argument is preceded by an identifier. Keyword arguments, also called named arguments, can be specified in any order. Keyword arguments are like name-value arguments in MATLAB.

Python SignatureMATLAB Usage

print(*objects,sep='',end='\n',
file=sys.stdout)

sep, end, and file are keyword arguments.

Change the value of end.
>> py.print('string',end='--')

This example uses the default value for the file keyword. Create some text variables and display the values.

x1 = py.str('c:');
x2 = py.os.curdir;
x3 = py.os.getenv('foo');
py.print(x1,x2,x3)
c: . None

To display the values on separate lines, use newline, \n, as a separator.

py.print(x1,x2,x3,sep=sprintf('\n'))
c:
.
None

Arbitrary Number of Keyword Arguments

Python defines an arbitrary number of keyword arguments by prepending the name with ** characters.

Python SignatureMATLAB Usage

dict(**kwarg)

>> D = py.dict(Joe=100,Jack=101)

Optional Arguments

An optional argument is a non-required argument.

Python SignatureMATLAB Usage

random.randrange(start,stop[,step])
Argument step is optional.

>> py.random.randrange(1,100)

Optional arguments can have default values. A default value is indicated by an equal sign = with the default value.

Python SignatureMATLAB Usage

print(*objects,sep='',end='\n',
file=sys.stdout)

The default value for file is sys.stdout.

Print two values using default keyword values.
>> py.print(2,'2')