Function Summary
" "1, what is a function function is a tool with a specific function 2, why use a function to reduce the expansion of repetitive Code enhancement program to enhance readability 3, how to use the function 1, function of the principle: first define after call (* * * *) definition phase: Only detect syntax does not execute code Call Stage: Execute function code 2, define phase and call stage: 2.1: Syntax (* * * *) def func (parameter 1, parameter 2,...): "" "" "" "" "" Code1 Code2 Code3 return value 2.2-shape participation argument (* * * *) The argument essence is the variable Mingshi is the value parameter of the variable: positional parameter def foo (x, y): Pass default parameter def foo (x,y=1): Pass *args def foo (X,*args): Pass **kwargs def foo (X,**kwargs): Pass argument: positional argument foo (1,2,3,4) keyword argument Foo (x=1,y=2,z=3) foo (x=1,1) # error foo (1,x=1) #错误 * Iterative Object Foo (* ' Hello') * * dictionary foo (**{' x ': 1, ' Y ': 2}) 2.3 Return value (* * * * *) returns is the function end flag, can have multiple RET within the function Urn, but as soon as the function executes once, it ends immediately and returns the value as the result of the call back to Def foo (x, y): return x+y Res=foo Note: 1, the return value can be any type 2, the return value does not have the number limit function inside no return: default return None Return value 1: Returns the value of return value 1, value 2, value 3: Return (value 1, value 2, value 3) 3, Function object (* * * *) def foo (): Pass function can be treated as data 1, reference F=foo 2, as the parameter print (foo) 3, as the return value def BA R (): Def wrapper () Pass return wrapper F=bar () 4, as a container class Type of Element Def F1 (): Pass def F2 (): Pass L=[F1,F2] L[0 ] () func_dic={' F1 ': F1, ' F2 ': F2} 4, Function nesting (* * * * *) nested invocation of 4.1 functions def foo () The nested definition of bar () F1 () F2 () foo () 4.2 function Def F1 (): def f2 (): Pass F2 () 5, namespace and scope (******) built-in namespace global name space local name Space def foo (x, y): #x =1,y=2 pass foo (ON) load order: Built-in, global-the order of local lookup names: from the current Position up look up if the current position is local-"global-" built-in X=1 def Outter (): Def wrapper (): Print (x) return wrapper f=outter () def bar (): x=111111 f () global scope: Global Survival, global effective built-in + global local scope: temporary survival, temporary effective local l=[] n=100 def foo (): L.appe nd (1111) global n n=1 def F1 (): X=1 def F2 (): Def f3 (): nonlocal x x=10 Globals () locals () 6, Closure function (* * *) X=1 D EF outter ():x=10 def wrapper (): print (x) return wrapper f=outter () 7, Adorner (* * *) d EF Deco (func): Def wrapper (*args,**kwargs): Res=func (*args,**kwargs) retutrn Res return wrapper def deco1 (x=1,y=2): Def deco (func): Def wrapper (*args,**kwargs) : Res=func (*args,**kwargs) retutrn Res return wrapper Retu RN Deco @deco def index (): Pass 8 Iterator (* * *) for Max min sorted Filter Map 9 Generator (* * *) 103-tuple-expression, list-derivation, dictionary-generated, generator-expression (* * * * *) The result of the res= condition is the result of the IF condition else condition set L =[expression for I in Iteration object if condition] g= (expression for I in Iteration object if condition) d={k:v for I in Iteration object if condition} 11 anonymous function (* * * * *) Lambda parameter 1, parameter 2: expression Max min Sorted map Filter 12 built-in function 13 function recursion (* * * *)" "defDeco1 (FUNC1):#Func1=wrapper2 defWrapper1 (*args, * *Kwargs):Print('First Adorner') Res1= Func1 (*args, * *Kwargs)returnres1returnWrapper1defDeco2 (FUNC2):#func2= The most primitive index defWrapper2 (*args, * *Kwargs):Print('a second adorner') Res2= Func2 (*args, * *Kwargs)returnRes2returnWrapper2@deco1#Index=deco1 (wrapper2) =wrapper1@deco2#Index=deco2 (index) =wrapper2defindex ():Print('From index') index ()" "first adorner second adorner from index" "View Code
Process-oriented programming
" "Process programming core process of the Word, process refers to the steps to solve the problem, that is, what to do first, what to do, and then what to do ... To write a program based on this idea is like designing an assembly line, which is a mechanical way of thinking. The complexity of the problems of the process, and then simplification of the shortcomings of the poor extensibility" "#receive user input user name, user name legality check, get legal user namedefCheck_user (): whileTrue:name= Input ('username>>'). Strip ()ifName.isalpha ():returnnameElse: Print('user name must be a letter, silly fork')#receive user input password, password legality check, get legal passworddefcheck_pwd (): whileTRUE:PWD1=input ('Password>>:'). Strip ()ifLen (PWD1) < 5: Print('password is at least 5 bits long') ContinuePWD2=input ('Again>>:'). Strip ()ifPWD1 = =PWD2:returnPWD1Else: Print('Two input passwords are inconsistent')defcheck_age ():Pass#pwd=check_pwd ()#print (PWD)#write a valid user name and password to the filedefInsert (user,pwd,age,path='Db.txt'): With open (Path,mode='a', encoding='Utf-8') as F:f.write ('%s:%s:%s\n'%(user,pwd,age))defRegister (): User=check_user () pwd=check_pwd () age=check_age () Insert (user,pwd,age)Print('Register successfull') Register ()#User function LayerdefRegister (): whileTrue:#Detecting user NamesName=input ('Username>>:'). Strip ()#detects if the user is repeating, re-enters if repeated, or breakres=check_user_interface (name)ifRes:Print('User Presence') Else: Break whileTrue:#Detect PasswordPWD1 = input ('Pwd1>>:'). Strip () Pwd2= Input ('Pwd2>>:'). Strip ()ifPwd1! =PWD2:Print('two times input password inconsistent, re-enter') Else: Breakdeftell_info (): Name=input ('>>:'). Strip () Info=Select (name)Print(Info)#Interface Layerdefcheck_user_interface (name): Res= Select (name)#res=[' Egon ', ' 123 '] ifRes:returnTrueElse: returnFalse#Data Processing LayerdefSelect (name): With open ('Db.txt','R', encoding='Utf-8') as F: forLineinchF:info= Line.strip ('\ n'). Split (':')#info=[' Egon ', ' 123 '] ifName = =Info[0]:returnInfoView Code
Use of modules
" " 1, what is the module module is a series of functions Common module form (custom module, third-party module, built-in module): 1, a module.py file is a module, the file name is module.py, The module name is Modules 2, a folder containing __init__.py files is also module 3, has been compiled into a shared library or DLL of C or c + + extension 4, written in C and linked to the Python interpreter of the built-in Module 2, why use the module 1, with the third party or the built-in module is a take doctrine, can greatly improve the development efficiency 2, the custom module will be in our own program needs to use the public function to write a python file and then the parts of the program components can be imported by the way to reference/ Reusing features in a custom module 3, how to import with modules There are two import module names from the module name import specific function " '
View Code
The search path for the module
a py file is a module, in the import must be found from a folder under the Py file Module search path refers to the folder that needs to be retrieved when importing the module the order in which the modules are looked up is: 1, first from the memory has been imported modules to find 2, built-in module 3, the environment variable Sys.path to find emphasis: Sys.path The first value is the folder where the current execution file is located
View Code
Two uses for Python files
One: can be used as a script to execute two: can be used as a module import
View Code
Directory Specification for Software development
Python-study-16