current position:Home>Python 3 development function

Python 3 development function

2022-05-15 02:32:50Dream of looking for flowers~~

1. General parameter def ada(x,y)      def add (×=4,y=5)
 It can receive both location parameters and keyword parameters .

2. Variable position tube parameters  def total (*args)
args Will collect 0 One or any arguments , Collect into an immutable tuple    *args * Modified formal parameters represent variable parameters , It can provide if the next   remainder 
 The argument of your pass parameter by bit , Will be collected into meta groups by formal parameters , Tuples are immutable 
*args  be called   Variable position parameter 
 Variable parameters can already be used 0 An argument , So you don't need a default value 

3. Variable keyword parameters  def fn (**kwargs) keyword arguments
** It's a modifier , Modify the following identifier , Used to kwargs
 Only the remaining keyword arguments can be collected 

 The identifier can be composed of letters 、 Numbers 、 Just make it up , But it can't start with a number 

4.keyword only  Keyword parameters only 
*args, Then the non deformable parameter 
*,  The identifiers that appear are generally keyword-only. except **kwargs

5. positional-only  Only positional parameters   3.8+
 At present, it is seldom used ,  Only individual function declarations use 

 Argument transmission :
(1) According to the position 
(2) keyword 
 It can be mixed , The location parameter must be passed before the keyword parameter 

 Insert picture description here

Parameter deconstruction can only be used in the bracket when the function is called .
Untie into Pass parameters by location

  • When providing arguments to functions , You can use... Before an iteratable object * perhaps ** To deconstruct the structure , Extract all the elements as the arguments of the function

  • Use * Solution composition position transmission parameter

  • Use ** The solution forms a keyword to pass parameters

  • The number of extracted elements should match the requirements of parameters

  • Local, Local scope 、 Local scope local Namespace . Function call , End of call

  • Enclosing,Python2.2 Introduced nested functions , Implemented closure , This is the namespace of the external function of the nested function

  • Global, Global scope , That is, the namespace of a module . The module is import Created on , The interpreter dies when it exits

  • Build-in, The namespace of the built-in module , The life cycle starts with Python Created when the interpreter starts to die when the interpreter exits . for example print(open),print and open Are built-in variables

So the search order of a noun is LEGB
 Insert picture description here


Call of single argument function + assignment

import time
import datetime

def logger(fn):
    def wrapper(*args, **kwargs):
        print(' Pre call enhancement ')
        start =
        ret = fn(*args, **kwargs)  # Parameters of deconstruction 
        print(' Post call enhancement ')
        delta = ( - start).total_seconds()
        print('Function {} took {}s.'.format(fn.__name__, delta))
        return ret
    return wrapper

def add(x, y):
    return x + y

print(add(100, 200))
def logger(fn):  # fn Namely add, logger Equivalent to a unique parameter 
    def wrapper(*args, **kwargs):  # wrapped(4,5) args=(4,5) {}
        print('add 4 5')
        print(' Call the... Executed before ,  enhance ')
        ret = fn(*args, **kwargs) # Closure  old add(4, 5) ——》9 
        print(' Executed after the call ,  enhance ')
        return ret
    return wrapper

@logger      #logger It's decorator grammar , Decorate the identifier below  add = logger(add)
# It brings up the following function identifier or identifier as logger The only argument to 
def add(x, y):
    return x + y

result = add(4, 5)

@logger Decorator syntax , The function is to change the name of the function or class decorated below / The identifier is proposed as logger The only argument to the expression  logger(add),
logger Function call , return wrapper,add amount to wrapper( Point to warpper The function object pointed to )
 After calling, it will be assigned to add, therefore add The identifier is no longer the original add, Has been transformed into warpper( The function object defined above ) 了 , Calling logger It's the time to have ,logger(add),add(x, y) That's what it's all about wrapper The ginseng ,fn The closure is preserved ( The original add)

logger The one that doesn't write anything at the back is called a nonparametric decorator , A parameterless decorator is equivalent to a single parameter function , Above fn You can write a parameter later ( Parameters do not need to be coriolised , The default value is ) in order to @logger # Equivalent to add=wrapper <=>add = logger(add), So in (fn) Looks like a one parameter function , We must get a single parameter function , and logger As a decorator, grammar can be written here ,logger(add) Will take the extraction below as a “ Single argument ”

 Insert picture description here

copyright notice
author[Dream of looking for flowers~~],Please bring the original link to reprint, thank you.

Random recommended