Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”
This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.
The 25th official Python column, stop! Don’t miss this zero-based article!
The previous article covered python functions and higher-order functions, and this article will supplement the rest of the function to make it a little easier.
Function parameter passing
Key-value:
Def show_info(name, title): print(" ",name) print(" ",title)Copy the code
We can use it in the following ways:
Show_info (' ray XueWei ', 'move brick masters') show_info (name =' ray XueWei 'title =' move brick masters')Copy the code
Dynamic length parameter transfer
def show_info_v2(name, title, *info): Print (" name ") print(" title ") print(" other comments: ", info)Copy the code
Can arguments be manipulated by functions?
Let’s take a look at the following procedure:
#! /usr/bin/env python # -*- coding: utF-8 -*- # @author: xuewei # @csdn /Juejin/Wechat: # @XueWeiTag: CodingDemo # @File: func_call.py # @Project: hello def compute_v1(list): sum = 0 for x in list: Sum += x list = list + [sum] print(" new address: ", id(list)) 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(" ", id(_list)) print(compute_v1(_list)) print(", _list) print(" ", id(_list) _list = [1, 2, 3, 4, 5] print(" id(_list) _list = [1, 2, 3, 5] print(" ", id(_list)) print(compute_v2(_list)) print(", _list) print(" id(_list))Copy the code
Here two compute functions change the address referenced by the parameter and change the value of the associated address (a variable) referenced by the memory space without changing the parameter reference.
They all worked. But the address of the _list outside is never changed unless a new address is assigned (i.e. copied before v2 is called).
Here is the result:
Other – Immutable parameters
If a datatype is passed in that cannot be changed in string, modifying the value based on a reference to the associated address (writing is prohibited), such as passing a tuple of underlying datatype elements as an argument, is prohibited in the call to the function. Python runs with an error!
By the way, if you like Python, please check out the Committee’s Python Basics column or Python Getting Started to Master column
Continuous learning and continuous development, I am Lei Xuewei! Programming is fun. The key is to get the technology right. Welcome to wechat, like support collection!