Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to pass function parameters in Python

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to pass the function parameters in Python, with a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

How to pass function parameters: pass parameters in key-value mode:

The following is a very common way of passing parameters. The parameter names are written bluntly and literally:

Def show_info (name, title): print ("name:", name) print ("title:", title)

We can use it in the following ways:

Show_info ('Lei Xue Committee', 'College Student Python Learning Community Director') show_info (name=' Lei Xue Committee', President of title=' continuous Learning Association')

It can be written like this and use two *, which is very flexible, but the disadvantage is also obvious (flexible structure sometimes needs to judge whether to miss transmission or not, and it is easy to make mistakes directly.)

Def show_info_v2 (* * kv_dict): print ("name:", kv_dict ['name']) print ("title:", kv_dict [' title']) show_info_v2 (name=' Lei Xue Committee', head of Python Learning Community for title=' College students')

Here is the effect, which looks the same.

Dynamic length parameter transfer

You usually use * plus a parameter name.

#! / usr/bin/env python#-*-coding: utf-8-*-# @ Time: 11:39 on 2021-10-24 # @ Author: LeiXueWei# @ CSDN/Juejin/Wechat: Ray Learning Committee # @ XueWeiTag: CodingDemo# @ File: func_call.py# @ Project: hellodef show_info (name, title): print ("name:", name) print ("title:", title) show_info ("Ray Learning Committee") Show_info (name=' Lei Xue Committee, President of title=' continuous Learning Association) def show_info_v2 (name, title, * info): print ("name:", name) print ("Professional title:", title) print ("other comments:", info) show_info_v2 ("Lei Xue Committee", "College Python Learning Community Director", "Love Technology" "Love life")

The running effect is as follows:

Will the parameter be changed by the function?

Let's take a look at the following procedure:

#! / usr/bin/env python#-*-coding: utf-8-*-# @ Time: 11:39 on 2021-10-24 # @ Author: LeiXueWei# @ CSDN/Juejin/Wechat: Ray academic Committee # @ XueWeiTag: CodingDemo# @ File: func_call.py# @ Project: hellodef compute_v1 (list): sum = 0 for x in list: sum + = x list = list + [sum] print ("New address:" Id (list)) return sumdef 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 ("before calling calculation function v1:", _ list) print ("memory address before calling calculation function v1:" Id (_ list) print (compute_v1 (_ list)) print ("after calling calculation function v1:", _ list) print ("memory address after calling calculation function v1:", id (_ list)) _ list = [1,2,3,4,5] print ("before calling calculation function v2:", _ list) print ("memory address before calling calculation function v2:" Id (_ list)) print (compute_v2 (_ list)) print ("after calling calculation function v2:", _ list) print ("memory address after calling calculation function v2:", id (_ list))

Here two compute functions, one modifies the parameter reference address, and the other does not modify the parameter reference but modifies the value of the associated address (a variable) of the reference memory space.

It can be said that they all succeeded. But the address of the outer _ list will not be changed at any time unless a new address is assigned (that is, it is copied again before v2 is called)

The following is the running result:

Thank you for reading this article carefully. I hope the article "how to transfer function parameters in Python" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report