Python Parameter Passing Note
我刚刚开始学习Python, Python中的参数传递总是让我很困惑。我写了4个简单的Demo,帮助我理解Python的参数传递,希望对大家都能有所帮助。
0:
def change(x):
x = 1
a = 10
print('a is {0}'.format(a))
change(a)
print('a is {0}'.format(a))
Output:
a is 10
a is 10
1:
def change1(x):
x = [1, 2] a = [10, 20]
print('a is {0}'.format(a))
change1(a)
print('a is {0}'.format(a))
Output:
a is [10, 20]
a is [10, 20]
2: [NOTE]We should pay more attention to this demo.
def change2(x):
x[:] = [1, 2, 4] a = [10, 20]
print('a is {0}'.format(a))
change2(a)
print('a is {0}'.format(a))
Output:
a is [10, 20]
a is [1, 2, 4]
3:
def change3(x):
x[0] = [1, 2, 4] a = [10, 20]
print('a is {0}'.format(a))
change3(a)
print('a is {0}'.format(a))
Output:
a is [10 20]
a is [[1, 2, 4], 20]]
对于参数传递,我总是用传值或者传址来理解,但在《Python基础教程》中建议我们用这样的方法来理解Python中的参数传递:(以Demo1为例说明)
1.#demo1 again:
2.def change1(x):
3. x = [1, 2]
4.
5.a = [10, 20]
6.print('a is {0}'.format(a))
7.change1(a)
8.print('a is {0}'.format(a)) #We can think it in the following way:
#5行:
a = [10, 20]
#7行:NOTE,用下面代码来理解参数传递
x = a
#3行:
x = [1, 2]
通过这样方法来理解参数传递,省去了我们的很多的考虑,个人感觉这种方法还是很不错的,不知道各位大神们是怎么理解Python中的参数传递的?
Python Parameter Passing Note的更多相关文章
- More about Parameter Passing in Python(Mainly about list)
我之前写了一篇关于Python参数传递(http://www.cnblogs.com/lxw0109/p/python_parameter_passing.html)的博客, 写完之后,我发现我在使用 ...
- Parameter Passing / Request Parameters in JSF 2.0 (转)
This Blog is a compilation of various methods of passing Request Parameters in JSF (2.0 +) (1) f:vi ...
- sysctl kernel parameter Optimization note
syncookies cookies the connection state,when the ack arrives,then deal with the pause connection,ver ...
- 【leetcode❤python】Ransom Note
#-*- coding: UTF-8 -*- class Solution(object): def canConstruct(self, ransomNote, magazine): ...
- Go parameter passing
package main import ( "fmt" ) func main() { fmt.Println("Hello, playground") var ...
- [python] File path and system path
1. get files in the current directory with the assum that the directory is like this: a .py |----dat ...
- Python面试常见的问题
So if you are looking forward to a Python Interview, here are some most probable questions to be ask ...
- python data analysis | python数据预处理(基于scikit-learn模块)
原文:http://www.jianshu.com/p/94516a58314d Dataset transformations| 数据转换 Combining estimators|组合学习器 Fe ...
- [转]Passing Managed Structures With Strings To Unmanaged Code Part 3
1. Introduction. 1.1 In part 1 of this series of blogs we studied how to pass a managed structure (w ...
随机推荐
- Echarts中线状图的X轴坐标标签倾斜样式
在echarts中应用线状图时可以展现很多的数据,而当数据量过多的时候,X轴的坐标就会显示不全,因为整个图形的宽度是一定的,X轴的全长是一定的 http://www.cnblogs.com/phpgc ...
- Nginx指令概述
指令概述 配置指令是一个字符串,可以用单引号或者双引号括起来,也可以不括.但是如果配置指令包含空格,一定要引起来. 指令参数 指令的参数使用一个或者多个空格或者TAB字符与指令分开.指令的参数有一个或 ...
- 基于quick-cocos2d-x的LuaSocket范例
这是一个 luasocket 范例. 为了便于使用,我封装了 luasocket 到 cc.net.SocketTCP 类中.这个范例展示如何使用 cc.net.SocketTCP . 同时,在本范例 ...
- php yaf框架扩展实践五——数据层
从狭义角度上来理解数据层就是数据库,比较广义的理解来看数据库.远程数据.文件等都可以看做数据层.项目初期的时候一般单一的数据库就可以了,随着流量的增大就要对数据层做很多的改进,例如增加从库分散读压力, ...
- mac上制作u盘启动盘
Mac上制作Ubuntu USB启动盘 一.下载ubuntu iso镜像 二.将iso转换为img文件 $ hdiutil convert -format UDRW -o /path/to/gener ...
- windows server 服务器添加免费域名证书的方法(Let's Encrypt)
在 windows server 服务器上可以通过 win-acme工具添加ssl 1.首先下载工具 https://github.com/PKISharp/win-acme/releases 最新版 ...
- 边缘检测sobel算子
sobel算子 - sophia_hxw - 博客园http://www.cnblogs.com/sophia-hxw/p/6088035.html #1,个人理解 网上查了很多资料,都说sobel算 ...
- boost数据结构tuple
boost数据结构tuple tuple(元组)定义了一个有固定数目元素的容器,其中每个元素类型可以不相同,这与其它容器有着本质的区别!vector和array虽然可以容纳很多元素,但是元素的类型必须 ...
- MySQL数据库安装文件夹与配置文件简易说明
1.MySQL安装文件夹 bin:存放着可执行文件 include:存放头文件 lib:存放库文件 share:存放字符集.语言等信息 2.配置文件 my.ini:MySQL软件正在使用的配置文件 m ...
- python3的安装,Window与linux
一.window安装 1.首先是window下的安装. 进入python网站https://www.python.org/downloads/下载页面,选择所需要的版本进行下载. 点击Download ...