我刚刚开始学习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的更多相关文章

  1. More about Parameter Passing in Python(Mainly about list)

    我之前写了一篇关于Python参数传递(http://www.cnblogs.com/lxw0109/p/python_parameter_passing.html)的博客, 写完之后,我发现我在使用 ...

  2. 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 ...

  3. sysctl kernel parameter Optimization note

    syncookies cookies the connection state,when the ack arrives,then deal with the pause connection,ver ...

  4. 【leetcode❤python】Ransom Note

    #-*- coding: UTF-8 -*- class Solution(object):       def canConstruct(self, ransomNote, magazine):   ...

  5. Go parameter passing

    package main import ( "fmt" ) func main() { fmt.Println("Hello, playground") var ...

  6. [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 ...

  7. Python面试常见的问题

    So if you are looking forward to a Python Interview, here are some most probable questions to be ask ...

  8. python data analysis | python数据预处理(基于scikit-learn模块)

    原文:http://www.jianshu.com/p/94516a58314d Dataset transformations| 数据转换 Combining estimators|组合学习器 Fe ...

  9. [转]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 ...

随机推荐

  1. (译)Getting Started——1.3.3 Working with Foundation(使用Foundation框架)

    在你使用Objective-C语言开发应用时,你会发现在开发中,你会用到很多框架.尤其是Foundation框架,该框架为应用提供了最基础的服务.Foundation框架包括了代表着基本数据类型的va ...

  2. Win7下 OpenCV+Qt开发环境搭建

    1.所需软件工具: (1)OpenCV开发库,2.4.9版:包括源文件(source文件夹)和编译后的文件(build文件夹),但最好自己使用CMake又一次编译.否则easy出错. (2)Qt Cr ...

  3. 基于jQuery的让非HTML5浏览器支持placeholder属性的代码(转)

    效果图:http://code.google.com/p/jquery-placeholder-js/ 演示代码:http://demo.jb51.net/js/2011/jqueryplacehol ...

  4. Flex远程访问获取数据--HTTPService

    编写service类: package services { import com.adobe.serialization.json.JSON; import log.LogUtil; import ...

  5. Linux平台使用Freetds连接SQL Server服务器,兼容PHP和Laravel

    本文在CentOS 7 64bit和Laravel 4.2环境测试通过.   1.下载源码并解压缩 wget ftp://ftp.freetds.org/pub/freetds/stable/free ...

  6. js 数组取出最大值最小值和平均值的方法

    1.直接遍历数组 ,,,,,,,]; ]; ;i<arr.length;i++){ if(max<arr[i]) max=arr[i]; } 2.借用Math的方法 ,,,,,,,]; v ...

  7. linux apache Tomcat配置SSL(https)步骤

    https简介 它是由Netscape开发并内置于其浏览器中,用于对数据进行压缩和解压操作,并返回网络上传送回的结果.HTTPS实际上应用了Netscape的安全套接字层(SSL)作为HTTP应用层的 ...

  8. Java容器:HashMap和HashSet解析

    转载请注明出处:jiq•钦's technical Blog 一.HashMap HashMap,基于散列(哈希表)存储"Key-Value"对象引用的数据结构. 存入的键必须具备 ...

  9. (转)负载均衡,回话保持,cookie

    servlet操作cookie:http://elf8848.iteye.com/blog/253198 负载均衡,回话保持:http://www.cnblogs.com/qq78292959/arc ...

  10. RAC集群节点故障模拟测试

    RAC节点故障模拟测试 重启单个RAC 节点模拟测试模拟操作步骤使用shutdown –Fr的方式重启节点,查看系统反应和数据库重新启动的时间.预期测试结果重启单个节点,vip将会切换到另外一个节点. ...