Python full stack __ Dynamic parameters, namespaces, scopes, scope chains, load order, function nesting, global, nonlocal

Source: Internet
Author: User

1. Dynamic Parameters

Parameters are passed normally when the number of arguments is equal to the number of formal parameters.

Def func1 (A, B, c):    passfunc1 (1, 2, 3)

When the number of arguments is not equal to the number of formal parameters, an error is encountered.

Def func1 (A, B, c):    passfunc1 (1, 2, 3, 4, 5, 6, 6)

When the number of arguments passed into the function is indeterminate, or for later expansion, the dynamic parameter *args **kwargs (universal parameter) is used at this time.

Order:

Positional parameters, *args, default parameters, **kwargs

1, *args

*args is to receive all positional parameters.

def func (*args, **kwargs):    print (args)    print (Kwargs) func (1, 2, 3, 4)

Note: The args in print () cannot be preceded with *.

2, **kwargs

The **kwargs receives all the keyword arguments.

def func (*args, **kwargs):    print (args)    print (Kwargs) func (x=4, y=5, z=6)

Note: The args in print () cannot be preceded with *.

def func (*args, **kwargs):    print (args)    print (Kwargs) func (1, 2, 3, x=4, y=5, z=6)

Order

Positional parameters, default parameters

Def Func2 (A, b, sex = ' male '):    print (a)    print (b)    print (Sex) func2 (1, 2, ' female ')

Positional parameters, *args, default parameters

Def Func2 (A, b,*args, sex = ' male '):    print (a)    print (b)    print (args)    print (Sex) func2 (1, 2, 5,6,7,8,9, sex= ' Female ')

Positional parameters, *args, default parameters, **kwargs

Def Func2 (A, B, *args, sex= ' man ', Age=20, **kwargs):    print (a)    print (b)    print (args)    print (Sex)    Print (age)    print (Kwargs) Func2 (1, 2, 5, 6, 7, 8, 9, x=6, y=5, Name= ' Alex ')

Adding device:

def sum1 (*args):    ret = 0 for    i in args:        ret + = i    return retprint (sum1 (1, 2, 3,))

def func3 (*args, **kwargs):    print (args)    print (kwargs) L1 = [1, 2, 3]l2 = [One, one, 32]func3 (L1, L2)

def func3 (*args, **kwargs):    print (args)    print (kwargs) L1 = [1, 2, 3]TU1 = (1, 2, 3) TU2 = (one,) func3 (*L1, *tu 1, *TU2)

def func3 (*args,**kwargs):    print (args) func3 (1, 2, 3, 1, 2, 3, 11, 22, 33)

In the definition of a function, * is the meaning of aggregation.

def func3 (*args,**kwargs):    print (args)    print (kwargs) L1 = [1, 2, 3]dic ={' name ': ' Alex '}dic1 ={' age ': 1000}func3 (*l1, **dic, **dic1)

* The use of Magic:

def func3 (*args,**kwargs):  # function definition * is intended to be aggregated.    print (args)    print (kwargs) L1 = [1, 2, 3]TU1 = (1, 2, 3) TU2 = (one, one, one) func3 (*L1, *TU1, *TU2)

def func3 (*args,**kwargs):    print (args)    print (Kwargs) func3 (1,2,3,1,2,3,11,22,33)

def func3 (*args,**kwargs):    print (args)    print (kwargs) L1 = [1, 2, 3]dic ={' name ': ' Alex '}dic1 ={' age ': 1000}func3 (*l1, **dic, **dic1)

2. Namespace, scope, scope chain, load order 1, Space name

2. Scope

3. Scope Chain

4. Loading order

3. Nesting of functions

4, Global, nonlocal 1, Global

2, nonlocal

Python full stack __ Dynamic parameters, namespaces, scopes, scope chains, load order, function nesting, global, nonlocal

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.