Function related content


1. What is a function
- a function is a block of code
2. Why use a function (defining a function)
- In order to achieve code reusability
- a function is a variable, which must be defined first, and then consumed
3. Classification of functions:
built-in functions: such as: Python built-in functions such as print, list, tuple, and dict can be called anywhere in python.
Custom functions: If the built-in functions cannot meet our needs for certain functions, we need to customize some functions
. 4. How to define functions
What does defining a function do?
- Only check the syntax, do not execute the code
Syntax def func():pass def keyword, func function name, () :
Define the function with parameters, and the application scenarios of the function with parameters
def func(name):pass - According to the external transmission Make logical judgments on the data of the
function Define no-parameter functions, and the application scenarios of no-parameter functions
def func():pass - only perform some operations, such as user interaction, printing, etc.
Define empty functions and application scenarios of empty functions
- Design code structure
5 、Calling a function
How to call a function
- first find the name, then call according to the name↓
- function name ()
function return value
- do not write the default return None
- return other (a value | tuple)
the return value of the function must have?
- Yes: call a function, after a series of operations, and finally get a clear result, you must have a return value
- No: ..., if you don't need to get any result, you don't need
an application with a return value function parameter : Formal and actual parameters, positional parameters, keyword parameters, default parameters, *arg, **kwargs
Three forms of function call:
- 1. Statement form: foo()
- 2. Expression form: 3*len( "hello")
- 3. When another function's parameter: range(len("hello"))
function parameter:
- Formal parameter and actual parameter definition
- Formal parameter is variable name, actual parameter is variable value, function call will The value is bound to the name, the function call ends, and the contact is bound. Note
: -
The default parameter is only assigned once when it is defined . Can be referenced - can be used as return value - can be used as container class element 7, function nesting - function inside function 8, scope and namespace - golbal - local - namespace: where the name is stored, three namespaces (x =1,1 is stored in memory, where x is stored is the namespace) - Loading order: namespace within the function → outer nested function → global → namespace of the built-in module 9, decorator - closure function: inner function contains a reference to the outer scope instead of the global scope














- The meaning of closure: The returned function object is not just a function object, but also wraps a layer of scope outside the function
- Application field: Delayed calculation
- Essence (closure function),
- Calling method (syntax)
- @function name =====>Syntax sugar
- Why use decorators
- Open and closed principle: closed to modification, open to extension
- adding new functions without modifying the source code
10, iterators, generators and coordinators Program Function
- Concept: Repeated process is called iteration, no repetition is one iteration, the end value of this iteration is the next initial value
- there is __iter__ method
- Advantages: lazy calculation, saving memory; Disadvantages: unable to To get the length, you can only go back
11, ternary operation, list comprehension, generator expression
- ternary operation: result='gt' if 1>3 else 'lt' print(result) # lt

- List comprehension: [(i*i) for i in range(10)]
- Generator expression (only given when used); syntax is the same as list push-down, except that [] becomes ()
- saves memory, once Only one value is generated
- Application: Read a large file and read it slowly, instead of bursting the
memory all at once Functions must have an end condition 13. Built-in functions - functions that exist inside python









Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325745819&siteId=291194637