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 ...
随机推荐
- 数据库设计(七)第三范式(3NF)
In our last tutorial, we learned about the second normal form and even normalized our Score table in ...
- C++Primer学习笔记《2》
数组是一种复合类型,由类型名+数组名+维度组成. 数组定义中的类型能够是C++基本内置类型.也能够是类类型的.数组元素的类型能够是除了引用类型以外的其它不论什么类型.没有全部的元素都是引用的数组. 数 ...
- 编写可维护的JavaScript----笔记(三)
1.块语句花括号的使用 在JavaScript中,注入if和for语句有两种写法,使用花括号包裹的多行代码或者不使用花括号的单行代码.但强烈建议不论是单行还是多行代码,都应该使用花括号. 2.花括号的 ...
- css 禅意花园 笔记
1. FOUC(Flash Of Unstyled Content) 现象( 在某些情况下,IE加载网页时会出现短暂的CSS样式失效. a: 只发生在Windows上的 IE (5.0版本以上) b ...
- 【BZOJ】3432: [Usaco2014 Jan]Cross Country Skiing (bfs+二分)
http://www.lydsy.com/JudgeOnline/problem.php?id=3432 题目说要相互可达,但是只需要从某个点做bfs然后判断其它点是否可达即可. 原因太简单了.... ...
- Android基础新手教程——3.1 基于监听的事件处理机制
Android基础新手教程--3.1.1 基于监听的事件处理机制 标签(空格分隔): Android基础新手教程 本节引言: 第二章我们学习的是Android的UI控件,我们能够利用这些控件构成一个精 ...
- 第10步:DBCA创建实例
注意,创建磁盘组时需要以oracle用户身份执行,在那之前可能需要以root身份执行xhost+,即命令: 代码1 [root@sgdb1~]# xhost+ [root@sgdb1~]# su - ...
- MySQL Error: Illegal mix of collations for operation 'concat'
在使用concat连接字符串时出现错误:MySQL Error: Illegal mix of collations for operation 'concat' 原因:字段操作默认为UTF8的编码, ...
- C#基础语言知识--编译和执行过程
http://blog.csdn.net/stive_sourcexin/article/details/51329697
- IOS开发报错之Undefined symbols for architecture armv6
本文转载至 http://blog.csdn.net/sanpintian/article/details/7575434 今天在项目中引入SVSegmentedControl.h/.my以及SVS ...