Talk about function passing and other supplements in Python

2022-01-30 19:37:42

This article has participated in  「 Digging force Star Program 」 , Win a creative gift bag , Challenge creation incentive fund .

ceremonial Python Column No 25 piece , Classmate, stop , Don't miss this from 0 The beginning of the article ！

The first part talked about python Functions and higher-order functions in , This article will supplement the other parts of the function , It's a little easier .

Function parameter passing

key-value Way to pass parameters ：

``````def show_info(name, title):
print(" The name is ：",name)
print(" The title is ：",title)
Copy code ``````

We can use it in the following ways ：

``````show_info(' Lei Xuewei ', ' Brick master ')
show_info(name=' Lei Xuewei ', title=' Brick master ')
Copy code ``````

Dynamic length parameter transfer

``````
def show_info_v2(name, title, *info):
print(" The name is ：", name)
print(" The title is ：", title)

show_info_v2(' Lei Xuewei ', ' Brick master ', " Love technology ", " love your life ")
Copy code ``````

Whether the parameter will be changed by the function ？

Let's take a look at the following program ：

``````#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/10/24 11:39  Afternoon
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat:  Lei Xuewei
# @XueWeiTag: CodingDemo
# @File : func_call.py
# @Project : hello

def compute_v1(list):
sum = 0
for x in list:
sum += x
list = list + [sum]
return sum

def compute_v2(list):
sum = 0
for x in list:
sum += x
list[0] = list[0] * 100
return sum

_list = [1, 2, 3, 4, 5]
print(" Call the calculation function v1 Before ：", _list)
print(" Call the calculation function v1 Previous memory address ：", id(_list))
print(compute_v1(_list))
print(" Call the calculation function v1 after ：", _list)
print(" Call the calculation function v1 Then the memory address ：", id(_list))

_list = [1, 2, 3, 4, 5]
print(" Call the calculation function v2 Before ：", _list)
print(" Call the calculation function v2 Previous memory address ：", id(_list))
print(compute_v2(_list))
print(" Call the calculation function v2 after ：", _list)
print(" Call the calculation function v2 Then the memory address ：", id(_list))
Copy code ``````

Here are two compute function , A modified parameter reference address , An associated address that does not modify the parameter reference, but modifies the reference memory space ( A variable ) Value .

It can be said that they all succeeded . But outside _list Your address will not be changed at any time , Unless you assign a new address （ That is, calling v2 Copy that time before ）

Here are the results ：

other - Immutable Parameters

If you pass in a data type that cannot be concatenated , So although the value is modified based on the reference association address （ No writing ）, For example, pass in a tuple composed of basic data type elements tuple As a parameter , Modification in the calling function is prohibited . Python An error will be prompted when running ！

by the way , like Python Friend, , Please pay attention to Python Basic column or Python From getting started to mastering the big column

Continuous learning and continuous development , I'm Lei Xuewei ！
Programming is fun , The key is to understand the technology thoroughly .
Welcome to wechat , Like support collection !