Python Full Stack development * 10 Knowledge Point Summary * 180612

Source: Internet
Author: User
Tags pear

10 Summary of function advanced knowledge points
I. Third of the dynamic parameter parameters
1. Dynamic Receive location parameter expression: *args (write * table in parameter location? receive any Content)
(1) Dynamic position parameters
Def eat (*args):
Print (args)
Eat ("fruit", "vegetable", "meat",)
# # Results are presented in the form of Ganso. : (' fruit ', ' vegetables ', ' meat ')
(2) position parameter, dynamic position parameter: Dynamic parameter must be after position parameter?
Def eat (A,b,*args):
Print (A,b,args)
Eat ("fruit", "vegetable", "chicken", "duck", "fish")
# # Results: Fruits and vegetables (' chicken ', ' duck ', ' fish ')
(3) If "*args", the position parameter, then the position parameter must be passed the value of the keyword parameter.
Def eat (*args,a,b,):
Print (Args,a,b,)
Eat ("Apple", "banana", "pear", "mango", a= "Lychee", b= "cherry")
# results: (' apple ', ' banana ', ' pear ', ' mango ') lychee cherry
(4). If there is a default value parameter, the default parameter must be written to the dynamic parameter before it takes effect.
Def eat (a,b,*args,c=66):
Print (A,B,ARGS,C)
Eat (1,2,3,4,5,6,7,8,)
# results: 1 2 (3, 4, 5, 6, 7, 8) 66
# dynamic parameter order: Positional parameter, dynamic parameter *, default value parameter
2. Dynamic receive keyword parameter expression: (**kwargs)
def fun (**kwargs):
Print (Kwargs)
Fun (a=11,b=22,c=33)
# The results are presented in the form of a dictionary; {' A ': one, ' B ': $, ' C ': 33}
# dynamic receive keyword parameter also after?
# Mixed
Def eat (A,b,*args,c=11,**kwargs):
Print (A,b,args,c,kwargs)
Eat ("Apple", "banana", "orange", "lychee", "Mango", "watermelon", d= "dragon fruit")
# dynamic parameter order: Position parameter, dynamic receive location parameter, default value parameter,. dynamic receive keyword parameter.
3. Invincible parameters: Def fun (*args,**kwargs) first position after the keyword
def fun (*args,**kwargs):
Print (Args,kwargs)
Fun (1,2,3,4,a=33,b=44,c=55)
# results (1, 2, 3, 4) {' A ': +, ' B ': +, ' C ': 55}
4. "Another method of parameter transfer for dynamic parameters
listing (list)
def fun (*args):
Print (args)
LST = [1, 4, 7]
Fun (Lst[0], lst[1], lst[2]) #或者下面的方式
Fun (*LST) # can make? * to break down a list in order
string
s = "I can't do it."
Fun (*s)
dictionary (dict)
def fun (**kwargs):
Print (Kwargs)
DIC = {' A ': 1, ' B ': 2}
Fun (**dic)
5. Comments on Functions
two. Namespaces: What do we give to the space that holds the relationship between a name and a value? Name: Namespace
1. Namespace classification:
# (1). Global namespace--we are directly in the PY, the variables declared outside the function belong to the global namespace
# (2) Local namespaces--variables declared in functions are placed in local namespaces
# (3) built-in namespaces--the names that the Python interpreter gives us, list, tuple, str, int these are built-in namespaces.
2. Loading order of namespaces
# (1) built-in namespaces;
# (2) Global namespace:
# (3) Local namespace (when function is executed)
3. The order in which namespaces are evaluated;
# (1) Local namespaces
# (2) Global namespace
# (3) built-in namespaces
three. Scope: As the scope of the work, according to the scope of the effectiveness of the global area as a local domain
1. Global domain: Contains built-in namespaces and global namespaces. Can be made anywhere in the entire piece? (Follow from top to bottom)
2. Local domain: Inside the function can be made?.
3.globals () function to view the contents of the global as field, locals () to see the variables and function information in the local field .
A = 10
def func ():
A = 40
b = 20
DEF ABC ():
Print ("haha")
Print (A, b) # This is the local area of the.
# Print (Globals ()) # Prints the contents of the global field
# print (Locals ()) # Prints the contents of a local field
Func ()
Four. Nesting of functions
1. If you encounter a () is the function of the tune? If there is no () is not the function of the tune?
2. Order of Functions
Def fun1 ():
Print (111)
Def fun2 ():
Print (222)
FUN1 ()
Fun2 ()
Print (111)
# results: 222,111,111
# Nesting of functions
Def fun2 ():
Print (222)
Def fun3 ():
Print (666)
Print (444)
FUN3 ()
Print (888)
Print (33)
Fun2 ()
Print (555)
# results: 33,222,444,666,888,555
four. Global and nonlocal
1.global table? Do not make the contents of the local domain anymore. Variables in the global domain
A = 100
def func ():
Global a # Adds a global representation that the variable is no longer locally created. ? is the direct cause of the global a
A=28
Print (a)
Func ()
Print (a)
# for variable data types you can go directly into the. But you can't change the address. Say it. Cannot assign a value
LST = ["Twist Vine", "Carina Lau", "James"]
def func ():
Lst.append ("? and") # can be accessed directly into the variable data type. But you can't change the address. Say it. Cannot assign a value
Print (LST)
Func ()
Print (LST)
2.nonlocal table? In the local field, adjust? Variable in the class namespace.
A = 10
Def func1 ():
A = 20
Def FUNC2 ():
Nonlocal A
A = 30
Print (a)
Func2 ()
Print (a)
Func1 ()
# J Result: Add nonlocal 30 30
# No Add nonlocal 30 20
#Multilayer Nesting
A = 1
Def fun_1 ():
A = 2
Def fun_2 ():
Nonlocal A
A = 3
Def fun_3 ():
A = 4
Print (a)
Print (a)
Fun_3 ()
Print (a)
Print (a)
Fun_2 ()
Print (a)
Print (a)
Fun_1 ()
Print (a)

Python Full Stack development * 10 Knowledge Point Summary * 180612

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.