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!

2

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

Comments

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 

Comments