Python NOTE 2: Functions

Source: Internet
Author: User
Tags function definition

5. Functions

The Python function code block starts with the DEF keyword; The function contents begin with a colon and are indented.
  * Note: python, strings, tuples, and numbers are objects that cannot be changed, while list,dict are objects that can be modified.
    The former transmits only the copy of the value, and does not affect the object itself; The latter passes the address of the object, changing the object itself

*5.1 Parameters:

1. Position parameter (required parameter):

When a function is called, the values passed in are assigned to each positional parameter in order of position


2. Default parameters:

def Power (x, n=2):   or   def enroll (name, Gender, age=6, city='Beijing' ):

* Note 1: setting is required only if it does not match the default parameters, which simplifies the complexity of function parameter invocation. Note: The default parameter must point to the immutable object!

* NOTE 2: when the function is called, the value of the default parameter is considered the default value if it is not passed in, (the default parameter setting, the default parameter must point to the invariant object)

  * Note 3: when you want to use a variable to do the operation, do not specify with the default parameter, the default address of the second call will not be re-instantiated one time


3. Variable parameter: *args

def Calc (*= 0  for in = SUM + N * n  Retu RN sum
You can use nums = [1, 2, 3] or (All-in-  one).  Calc (*nums) adds an * to the list or tuple before changing the elements of the list or tuple into variable parameters.

  

4. Keyword parameter:**kw extended function function
  (Scene: User registration, in addition to the user name and age is required, the other is optional, using the keyword parameters to define this function can meet the requirements of registration.) )

def person (name, age, * *kw):print('name:'age:  "other:", kw)
You can pass in any number of keyword parameters: person ('Adam', gender='M', job='  Engineer')


* Note 1:*args is a variable parameter, and args receives a tuple;
**KW is the keyword parameter, and kw receives a dict.
Variable parameters can be passed directly: Func (1, 2, 3), or the list or tuple can be assembled first, and then passed through *args: Func (* (1, 2, 3));
Keyword parameters can be directly passed in: Func (A=1, b=2), can be assembled dict, and then passed through **kw: func (**{' a ': 1, ' B ': 2}).
Using *args and **kw is a Python idiom, but it can also be used with other parameter names, but it's best to use idioms.

* NOTE 2: The order of the parameters is inconsistent with the declaration when using the keyword parameter to allow the function call;

5. Named keyword parameter: Special separator *, * the following parameter is treated as a named keyword parameter, restricting the name of the parameter that the caller can pass in. accept only city and job as keyword parameters

def person (name, age, *, City, Job):print(name, age, city, job)

If there is already a mutable parameter in the function definition, then the named keyword argument will no longer need a special delimiter *:

def person (name, age, *args, City, job):print(name, age, args, city, job)


6. Parameter combination:
The order of the parameter definitions must be: required, default, variable, named keyword, and keyword parameters.
For any function, it can be invoked in the form of a func (*args, **kw), regardless of how its arguments are defined.


7. Global variables and local variables
Variables defined inside a function have a local scope, which defines the owning global scope outside of the function.

refer to Liao Xuefeng Tutorial:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/ 001431752945034eb82ac80a3e64b9bb4929b16eeed1eb9000

Python NOTE 2: Functions

Related Article

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.