Parameter and parameter deconstruction of Python function

Source: Internet
Author: User
Tags function definition

Function
    python function consists of:         definition statement, function name, parameter list, code block       function:         encapsulates a function code to facilitate reuse .          code More Concise classification of .     functions:          Built-in functions such as: , Max ()          library functions:  such as, Math.ceil (), etc.    Definition of    function:         def statement definition function (to be used after defined function)          def functionname ([args_list]):              code block             [ The specification and identifier specification for the return value]         function name is the same .          Indent Convention is 4 Spaces         return statement can be omitted, but its default return value is None          Example:             DEF FN (x):                 return x   The call to the    function:         This function by the function name, functionname ([args_list])   The         function is a callable object, and the callable () function checks whether this object can be called           Example:             FN (1)
Parameters


Parameters: Parameter arguments used when a function is defined: parameter arguments to use when a function is called: arguments are passed to the parameter default value one at a time, according to the matching rules of the formal parameter: when defined, the function is followed by a value after the parameter (no default value for the variable parameter): When the argument is not When given sufficient value, the default parameter is too large to be assigned to a given parameter, simplifying the function call


Formal parameters are: positional parameter, variable position parameter, variable keyword parameter, keyword-only parameter argument: positional parameter, keyword parameter


     parameter:         positional parameters, which can accept one argument and match from left to right according to the order of arguments passed              representation:  Parameter name, example:  DEF FN (x)          variable position parameters, can accept any positional arguments (including 0), after the position parameters are taken out, the rest are received               notation:  ' * ' followed by parameter name, example:  def fn (*args)               a set of positional parameters to form a tuple          variable keyword parameters, can accept any keyword arguments (including 0), after the keyword parameters are taken out, the rest are received               Representation:  ' * * ' followed by parameter name, example:  def fn (**kwargs)               form a dictionary of keyword parameters          Keyword-only parameter, normal parameter after variable position parameter, can accept one argument, match argument in left and right order               means:   &nbsP;              1. ' * ',  heel general parameters                       Example:  def fn (*,x,y)  , x and y are keyword-only parameters                  2. Variable position parameters followed by normal parameters                       Example:  DEF FN (*args,x,y)  , x and y are keyword-only parameters


Argument: Positional parameter: Can be a collection object that iterates over an object and can be viewed as a single object of a whole. For example: (+), {1,2},[1,2],list (1,3)), 1 keyword parameter: name = parameter of value format. Example: ' A ' = 1


Parameter definition Order rules: Positional Parameters--positional parameters (with default values)--Variable position Parameters--keyword-only Parameters--keyword-only parameters (with default values)--Variable keyword parameters Parameter order: Positional parameter----keyword argument real participation parameter matching rule: When a positional argument is passed, the formal parameter matches the key argument from left to right, and the parameters are matched according to the keyword, regardless of the order.


The difference between a dynamic language and a static language function definition: The formal parameters of a dynamic language's function are of no type, so there is no error when passing arguments. The formal parameters of a static language function are of a type, so when the argument is passed, it is directly an error.


Parametric deconstruction (deconstruction of actual parameters)
Deconstructed object collection type can iterate over the object to deconstruct the symbol * and * * The solution of the Non-dictionary-type deconstruction object to form a positional parameter * * The number of arguments to be extracted from the solution of the dictionary type to the key parameter is matched with the number of parameters, and the actual parameter type is matched


This article is from the "12064120" blog, please be sure to keep this source http://12074120.blog.51cto.com/12064120/1973029

Parameter and parameter deconstruction of Python function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.