Python function basics 2: python function Basics
1. What are naming keyword parameters?
Format: All parameters after * are named keyword parameters.
Features: 1. The caller of the constrained function must pass the value in the form of Kye = value.
2. The caller of the constraint function must use the specified Key name.
def auth(*args,name,pwd): print(name,pwd)auth(pwd='213',name='egon')def register(name,age): print(type(name),type(age))register(123,[1,2,3])
### Output:
Egon 213
<Class 'int'> <class 'LIST'>
Ii. Function nesting
1. nested call of functions: other functions are called in the function.
def max2(x,y): if x > y: return x else: return ydef max3(x,y,z): res1=max(x,y) res2=max(res1,z) return res2print(max3(88,99,100))
### Output:
100
2. nested Function Definition: other functions are defined in the function.
Def func1 (): print ('from func1') def func2 (): # func2 = memory address print ('from func2') print (func2) # <function func1. <locals>. func2 at 0x0000024907A098C8> func2 () func2 () func2 () func1 ()
### Output:
From func2
From func2
From func2
Iii. Function namespace
1. namespace: the place where the name and value are bound
x=888888888def func(): pass
2. There are three types of namespaces.
(1) built-in namespace: stores the name of the python interpreter, which takes effect when the interpreter is started, and the interpreter is disabled.
(2) global namespace: a file-level name that takes effect upon execution of the file. It becomes invalid when the file is terminated or deleted during execution.
x=1def f1(): def f2(): print(x) f2()f1()if 10 > 3: y=33333while True: xxxxx=123123123
### Output:
1
(3) local namespace: stores the custom name of the function (both the parameters of the function and the names in the function are stored in the local namespace), which takes effect temporarily when the function is called, the function is invalid when it ends.
Note:
Loading Sequence: built-in namespace --------> global namespace ------- >>> local namespace
Search name: local namespace --------> global namespace ------- >>> built-in namespace
def f1(): # len=1 def f2(): # len=2 print(len) f2()f1()
### Output:
Global
3. Scope
Global scope: contains the names of the built-in and global namespaces.
Features:
(1) access
(2) the name in the range will be accompanied by the entire life cycle of the program.
Local scope: contains the name of a local namespace.
Features:
(1) only used in Functions
(2) The call takes effect when the function is called, and the call ends and becomes invalid.
Iv. Function objects
1. functions are the first type of objects in python.
(1) can be referenced
x=1y=xdef bar(): print('from bar')f=barf()
### Output:
From bar
(2) can be passed as a parameter
x=1def func(a): print(a)func(x)
### Output:
1
(3) can be used as the return value of a function.
# Code (1)
X = 1def foo (): return xres = foo () print (res)
### Output:
1
Code (2)
Def bar (): print ('from bar') def foo (func ): # func = <function bar at 0x00000225AF631E18> return func # return <function bar at 0x00000225AF631E18> # print (bar) f = foo (bar) # f = <function bar at 0x00000225AF631E18> # print (f) f ()
### Output:
From bar
(4) elements that can be used as container types
x=1l=[x,]print(l)def get(): print('from get')def put(): print('from put')l=[get,put]# print(l)l[0]()
### Output:
[1]
From get
Note:
1. func can be referenced
F = func
2. func can be passed to x as a parameter.
3. func can also be used as the return value.
4. It can be used as a type element in the container.
Reference of the function query logon function:
Def auth (): print ('Log On: ') def reigster (): print ('register:') def search (): print ('view :') def transfer (): print ('transfer') def pay (): print ('pay') dic = {'1': auth, '2': reigster, '3': search, '4': transfer, '5': pay} def interactive (): while True: print ('''1 authentication 2 Registration 3 View 4 transfer 5 Payment ''') choice = input ('enter number :'). strip () if choice in dic: dic [choice] () else: print ('invalid operation') interactive ()
5. Closure Functions
Closed: refers to the function defined inside the function.
Scope relationship. It is specified to be dead in the function definition stage and has nothing to do with the call location.
def outter(): x=2 def inner(): # x=1 print('from inner',x) return innerf=outter() #f=innerdef foo(): x=1111111111111111111111111111 f()foo()
### Output:
From inner 2
1. Closure functions:
(1) Functions Defined inside the Function
(2) and the function contains a reference to the name in the external function scope. This function is called a closure function.
Understanding:
Method for passing values to Functions
Method 1: Pass the value as a parameter
Use crawlers to obtain the website source code:
import requests:def get(url): response=requests.get(url) if response.status_code == 200: print(response.text)get('https://www.baidu.com')
Method 2
import requestsimport timedef outter(url): #url='https://www.baidu.com' # url='https://www.baidu.com' def get(): response=requests.get(url) if response.status_code == 200: print(response.text) return getbaidu=outter('https://www.baidu.com')python=outter('https://www.python.org')baidu()print('=====================>')time.sleep(3)baidu()