Python's basic Learning 12 days, function

Source: Internet
Author: User

1, *args dynamic parameters, universal parameters

*args is to accept the remaining positional parameters corresponding to the arguments and place them in the tuple. When defining a function, *args represents an aggregation.

def func (*args):    print(args) func (1,34,'jdai' ) , 5,56,66) # (1, Jdai, ' 5, 56, 66)
defFunc (a,b,c,d,*args,e='male'):    Print(a)Print(b)Print(c)Print(d)Print(args)Print(E) func (1,2,3,4,5,10,5,6,7,8,e='female')#1
2
3
4
(5, ten, 5, 6, 7, 8)
female

2,**kwargs, dynamic parameter, accepts only keyword parameters, and puts all keyword parameters (meaningless) into a dictionary.

Final order (formal parameter angle)

def func (a,b,c,**Kwargs):    print(Kwargs) func (1,3,4,r=6,t=7,y=0)

Positional parameters, *args, default parameter **kwargs

defFunc (a,b,c,d,*args,e='male',**Kwargs):Print(a)Print(b)Print(c)Print(d)Print(args) # (5, ten, 5, 6, 7, 8)Print(e)Print(Kwargs) #{' F ': ' female ', ' h ':, ' G ': 5}func (1,2,3,4,5,10,5,6,7,8,f='female', g=5,h=48)

3, * The magic of the use, when the function call execution, * represents the break. * Add an iterator to the object (List,tuple,set,str,dict's key), representing the split, adding element one by one to args.

def func (*args):    print= [1,2,3,4,6= [3,6,6,7,77,66= ( ' EQWJ ','ee','UI') func (*li,* L2,*TU)

(1, 2, 3, 4, 6, 3, 6, 6, 7,,, ' eqwj ', ' ee ', ' ui ') add element one by one to args.

defFunc (* *Kwargs):Print(Kwargs) dic= {'name':'Alex',' Age': 44}dic1= {'Home':'Taiwan','Hobby':'Football'}func (**DIC,**DIC1)

* * Dictionary, the representative of the broken, put all the key-value pairs into a Kwargs dictionary.

defFunc (*args,**Kwargs):Print(args)Print(Kwargs) Dic1= {'honnn':'Shui','Nugu':'DAO'}dic2= {'name1':'2.5',' Age': 33}func (*[1,3,4,5],*'ahdhsdkj', **dic1,**dic2)

(1, 3, 4, 5, ' a ', ' h ', ' d ', ' H ', ' s ', ' d ', ' K ', ' J ')
{' Nugu ': ' DAO ', ' name1 ': ' 2.5 ', ' honnn ': ' Shui ', ' age ': 33}

4, namespace, also called namespaces. Place the variable with the corresponding worthy relationship (the memory address of the relationship). When executing a function, it opens a temporary namespace in memory, storing all the variables in the function body as well as the worthy relationship, as the function executes,

The temporary space is automatically closed.

Global namespaces, namespaces, namespace, full namespace.

A local namespace, a temporary namespace, a temporary name space, and a local name space.

Built-in namespaces, all built-in functions, and so on storage space.

Load Order:

Built-in namespaces >>> Global namespaces >>> (when function executes) local namespaces

Order of Values:

Local namespaces >>> Global namespaces >>> built-in namespaces (when function executes)

' old boy ' def func1 ():     ='taibai'    print(name) func1 () #taibai
' old boy ' def func1 ():     # name = ' Taibai '    Print (name) func1 () # old boy

Global scope: Global namespace, built-in namespace.

Local scope: Local namespace.

5, nesting of functions

Print(111)#1deffunc1 ():Print(333)#2Func2 ()Print(777)#4defFunc2 ():Print(444)#3deffunc3 ():Print(555) Func2 () func1 ()Print(222)#5
Print(111)#1deffunc1 ():Print(333)#2Func2 ()Print(444)#6defFunc2 ():Print(222)#3func3 ()Print(555)#5deffunc3 ():Print(666)#4func1 ()Print(777)#7

6,globals and locals

A = 2= 3def  func1 ():    = 4    = 5    print(Globals ())#  Place the global variable in the dictionary    return locals ()# Place the local variable in the dictionary {' C ': 4, ' d ': 5}print( Func1 ())

{' A ': 2, ' __cached__ ': None, ' __file__ ': ' d:/python3/day11/xuxi329.py ', ' __name__ ': ' __main__ ', ' __doc__ ': none, ' __ Builtins__ ': <module ' builtins ' (built-in), ' B ': 3, ' __loader__ ': <_frozen_importlib_external. Sourcefileloader object at 0x000001ee02a95780>, ' func1 ': <function func1 at 0x000001ee026ca048>, ' __spec__ ': None, ' __package__ ': none}

A = 4def  func1 ():    = 6    def  Func2 ():        nonlocal b#  Nonlocal cannot modify global variable        b = 666        print(b)    Func2 ()    print(b) Func1 ()

Global

1, within the local space, declare a global variable.

2, change a global variable within the local space.

Globals, global namespace: all variables.

Locals, local namespace: all variables.

Nonlocal:

1, global variables cannot be modified.

2, in the local scope, the variables of the parent scope (or the outer scope non-global scope) are referenced and modified, and which layer of the reference, from that layer and the following this variable all changed.

Python's basic Learning 12 days, function

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.