This is the fifth day of my participation in Gwen Challenge

Functions are created to improve application modularity and code reuse.

1. The concept

The standard structure of the function is as follows:

Def < function name > (< function argument >):Copy the code

2. Parameter transfer

2.1 Variables & Objects

In Python, variables have no type, only objects have type

A = 'stone' list= [1,2,3]Copy the code

In the above code, ‘stone’ is a String, and [1,2,3] is a List. The variables a and List are only applied to one object. They are analogous to Pointers in C.

2.2 Mutable objects and Immutable Objects

Python variables hold references to objects that are in the heap. Objects allocated in the heap are divided into two categories: mutable and immutable.

Python3 is known for its six standard object types:

  • ** Immutable object types (3) : **Number, String, and Tuple;
  • ** Mutable object types (3) : **List, Dictionary, Set.

For immutable objects, the value in memory to which a variable points cannot be changed

int_val=5 
int_val=10
Copy the code

In the first line of code, int_val refers to a new space on the heap, the integer 5. In the second line, int_val refers to a new space on the heap, the integer 10

The built-in function id(int_val) can be used to print the address of the memory space twice

For mutable objects, the value in the memory to which the variable points can be changed

List_val = [1, 2, 3] list_val [1] = 4Copy the code

In the first two lines of code, list_val can be changed, which directly changes the value in the memory to which it refers

List_val (list_val); list_val (list_val)

2.3 Function Parameter Passing

Python allows you to pass a reference to an argument, that is, to the memory that the original variable actually points to. When you modify the memory, you will modify the contents of the memory according to the reference.

Explained by the following code:

Def val_change(int_val,str_val,list_val): int_val = int_val+1 str_val = str_val + 'd' list_val[1] = 1 return int_val, str_val, list_valCopy the code
[int_b, str_B, list_b] = val_change(int_a, str_a, list_b, list_a) print('int_a = ', int_a) print('int_b = ', int_b) print('str_a = ', str_a) print('str_b = ', str_b) print('list_a = ', list_a) print('list_b = ', list_b)Copy the code

The output

int_a =  1
int_b =  2
str_a =  ac
str_b =  acd
list_a =  [0, 1]
list_b =  [0, 1]
Copy the code

From the above examples, it can be concluded that:

  1. If a function parameter is an immutable object, when the function body modifies the object, it modifies the copied object and does not affect the parameters outside the function
  2. For a function whose parameters are mutable objects, changes made by the function body to the object directly affect the parameters outside the function

Iterators and generators

3.1 the iterator

An iterator is an object that remembers the position of the iterator. Iterator objects are accessed from the first element of the collection until all elements have been accessed. Iterators can only move forward and not backward.

use

  1. Iter (< iterable >)Creates iterator objects, iterable objects includestring.The list oforA tuple object, or a custom iterator
  2. Next (< iterator object >), the next element of the iterator

Specific use steps:

List =[1,2,3] iterator = iter(list) # print(next(iterator)) # print the next element of the iterator, output 1Copy the code

Pay attention to

With the built-in function next(), it is possible to raise the StopIteration exception, which is used to mark the completion of an iteration and prevent an infinite loop.

Custom iterator objects

Using a class as an iterator requires implementing two methods in the class: __iter__() and __next__().

3.2 the generator

A function that uses yield is called a generator. A generator is a function that returns an iterator and can only be used for iterative operations. Call a generator function that returns an iterator object.

The principle of

During a call generator run, each time yield is encountered, the function pauses and saves all current run information, returns the yield value, and continues from the current position on the next execution of the next() method

use

See the following code

Rangedef custom_range(start,end,step): x = start while x < end: yield x += stepCopy the code
For I in range(1,10,0.5): print(I)Copy the code

4.lambda

Use lambda to create anonymous functions

use

Lambdadef add(x,y): return x+yCopy the code
Lambdaadd =lambda x,y:x+yCopy the code

5. The scope

Variables defined inside a function have a local scope, and variables defined outside a function have a global scope. The global keyword declares variables that are globally applied to.

Explain through the following examples:

Count = 0 def count_num(): Print ('in function ', count, index) print('out function ', count, index)Copy the code

The output

in function  1 2out function  1 0
Copy the code

6. Simple function parameters