1

At the end of the day studying parameters and arguments in python , I finally made the following conclusion

Order of parameters in function definition

def foo ( non-optional parameters , optional parameters , *args , **kwargs):

Order of arguments in function call

foo( non-keyword arguments , keyword arguments )

Just want to know whether there is any exception to this in python world.... Further if there are any other types of arguments/parameters please do comment...

Thanks!

ndpu
  • 22,225
  • 6
  • 54
  • 69
  • possible duplicate of [Python normal arguments vs. keyword arguments](http://stackoverflow.com/questions/1419046/python-normal-arguments-vs-keyword-arguments) – YXD Mar 07 '14 at 10:10
  • 1
    Allowed syntax for [Python 2.x](http://docs.python.org/2/reference/compound_stmts.html#function-definitions) and allowed for [Python 3.x](http://docs.python.org/3/reference/compound_stmts.html#function-definitions) - note the difference for specifying end of positional arguments in Python 3.x is discussed in [PEP 3102](http://legacy.python.org/dev/peps/pep-3102/) – Jon Clements Mar 07 '14 at 10:24

2 Answers2

2

Argument Matching Syntax summarizes the syntax that invokes the special argument-matching modes. Function argument-matching forms Syntax Location Interpretation

  1. func(value) Caller Normal argument: matched by position
  2. func(name=value) Caller Keyword argument: matched by name
  3. func(*iterable) Caller Pass all objects in iterable as individual positional arguments
  4. func(**dict) Caller Pass all key/value pairs in dict as individual keyword arguments

below are for defining functions......

  1. def func(name) Function Normal argument: matches any passed value by position or name
  2. def func(name=value) Function Default argument value, if not passed in the call
  3. def func(*name) Function Matches and collects remaining positional arguments in a tuple
  4. def func(**name) Function Matches and collects remaining keyword arguments in a dictionary
  5. def func(*other, name) Function Arguments that must be passed by keyword only in calls (3.X)
  6. def func(*, name=value) Function Arguments that must be passed by keyword only in calls (3.X)
Pavan Gupta
  • 17,663
  • 4
  • 22
  • 29
0

The Python Language Reference describes the syntax and “core semantics” of the language. It is terse, but attempts to be exact and complete. There you find descriptions of lexical analysis and syntax using a modified BNF grammar notation.

Usually all Python implementation follow this grammar. Thus there should be no exceptions on this grammar. If yes, you should refer to the implementation-specific documentation.

Function definitions

decorated      ::=  decorators (classdef | funcdef)
decorators     ::=  decorator+
decorator      ::=  "@" dotted_name ["(" [argument_list [","]] ")"] NEWLINE
funcdef        ::=  "def" funcname "(" [parameter_list] ")" ":" suite
dotted_name    ::=  identifier ("." identifier)*
parameter_list ::=  (defparameter ",")*
                    (  "*" identifier ["," "**" identifier]
                    | "**" identifier
                    | defparameter [","] )
defparameter   ::=  parameter ["=" expression]
sublist        ::=  parameter ("," parameter)* [","]
parameter      ::=  identifier | "(" sublist ")"
funcname       ::=  identifier

Calls

call                 ::=  primary "(" [argument_list [","]
                          | expression genexpr_for] ")"
argument_list        ::=  positional_arguments ["," keyword_arguments]
                            ["," "*" expression] ["," keyword_arguments]
                            ["," "**" expression]
                          | keyword_arguments ["," "*" expression]
                            ["," "**" expression]
                          | "*" expression ["," "*" expression] ["," "**" expression]
                          | "**" expression
positional_arguments ::=  expression ("," expression)*
keyword_arguments    ::=  keyword_item ("," keyword_item)*
keyword_item         ::=  identifier "=" expression
wolfrevo
  • 6,651
  • 2
  • 26
  • 38