(Python) Function calls for beginners

A function `call` is a callable object with a series of arguments which can be empty.

syntax : primary(arg_list['',''])
arg_list ::= positional_args [, keyword_ags][, *expression][, ** expression] |
keyword_args[, *expression][, ** expression] |
*expression [, **expression] |
** expression

positional_args ::= expression (, expression*)
keywords_args ::= keyword_item (, keyword_item*)
keyword_item ::= identifier = expression

`primary` is the callable object (function name, built-in callable method objs, class objs, methods of class instances and callable class instances)

How arguments are processed ?

1. a list of unfilled slots for formal parameters is created
2. N positional_args are placed in first N slots
3. keyword_args are placed in corrsponding slot using identifier

TypeError if a slot is already filled

4. Unfilled slots are filled with corresponding default values

TypeError if slots exist with no default values
TypeError if positional_args > formal param slots
TypeError if ketyword_args doesn’t match with formal param name unless **identifier is present

5. *expression evaluates to sequence like y1…yM and are placed as additional positional_args after x1…xN such that the call is made with x1….xN,y1..yM positional arguments
6. **expression is a dictionary contained excess keyword arguments

TypeError if **expression doesn’t evaluate to dictionary

7. *expression is processed before keyword arguments, hence unusual to use both keyword_args and *expression

A call return can be,

  • as per the return statement for a user-defined func
  • up to the interpreter for a build-in func or method
  • a new class instance for a class object
  • the user-defined function for a class instance method with the instance as the first argument
  • as per __call__() method for a class instance

Examples:

# define a function with two params, printing them as result
>>> def f(a,b): print a, b
...

# both are keyword_args
>>> f(a=1,b=2)
1 2

# both are positional_args
>>> f(1,2)
1 2

# a is keyword and b is considered positional. ERROR!
>>> f(a=1,2)
File "<stdin>", line 1
SyntaxError: non-keyword arg after keyword arg

# a is positional and b is keyword_arg
>>> f(1,b=2)
1 2

# 1 is positional while 2 is *expr and takes as additional positional_arg for b
>>> f(1,*(2,))
1 2

# 1 is keyword for a, but 2 is also positional_arg for a. ERROR!
>>> f(a=1,*(2,))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: f() got multiple values for keyword argument 'a'

# 1 is a keyword_arg for b, while 2 is positional_arg for a
>>> f(b=1,*(2,))
2 1

# 1 is keyword_arg for a, while 2 is specified thru **expr for b
>>> f(a=1,**{'b':2,})
1 2

# 1 is positional_arg using *expr for a, b is keyword_arg thru **expr for b
>>> f(*(1,),**{'b':2,})
1 2

# both a and b are specified as keyword_args thru **expression
>>> f(**{'a':1,'b':2,})
1 2

Original Reference (Python Reference Documentation)

Advertisements

One thought on “(Python) Function calls for beginners

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s