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

写完之后,我发现我在使用list的时候(我想在函数中改变实参),感觉使用文章中提到的传参理解还是有点儿迷惑和混乱

所以在此关于list的参数传递,再做一下补充和说明,这些是我个人的理解,如果您感觉有任何疑问或者不同的观点,非常

感谢您与我讨论,谢谢。

#!/usr/bin/python
#coding:utf-8
#File: listParaPass.py
#Author: lxw
#Time: 2014-04-19
#Usage: Learn more about parameter passing in Python. # 所以得到的结论就是:想改变实参,则实参不能以分片的形式传递,且函数内部须以分片的形式操作 def change(x):
x[:] = ['o', 'k']
print('x is {0}'.format(x)) #1:
a = [10, 20, 30]
print('a is {0}'.format(a)) # a is [10, 20, 30]
change(a) # x is ['o', 'k']
print('a is {0}'.format(a)) # a is ['o', 'k'] #2:
a = [10, 20, 30]
print('a is {0}'.format(a)) # a is [10, 20, 30]
# 想改变实参,则不能传分片;使用分片传,不会影响到实参。
change(a[:]) # x is ['o', 'k']
print('a is {0}'.format(a)) # a is [10, 20, 30] print('')
print('') def change1(x):
# 与上面的例子对比得到如下结论:
# 想改变实参(前提实参传的不能是分片),则函数内须用分片;若函数内不使用分片,则不会影响到实参。
x = ['o', 'k']
print('x is {0}'.format(x)) #1:
a = [10, 20, 30]
print('a is {0}'.format(a)) # a is [10, 20, 30]
change1(a) # x is ['o', 'k']
print('a is {0}'.format(a)) # a is [10, 20, 30] #2:
a = [10, 20, 30]
print('a is {0}'.format(a)) # a is [10, 20, 30]
change1(a[:]) # x is ['o', 'k']
print('a is {0}'.format(a)) # a is [10, 20, 30] print('')
print('') def change2(x):
x[1:3] = ['o', 'k']
print('x is {0}'.format(x)) #1:
a = [10, 20, 30]
print('a is {0}'.format(a)) # a is [10, 20, 30]
change2(a) # x is [10, 'o', 'k']
print('a is {0}'.format(a)) # a is [10, 'o', 'k'] #2:
a = [10, 20, 30]
print('a is {0}'.format(a)) # a is [10, 20, 30]
change2(a[:]) # x is [10, 'o', 'k']
print('a is {0}'.format(a)) # a is [10, 20, 30]

 注:把索引(如:a[1])也归入分片操作。

More about Parameter Passing in Python(Mainly about list)的更多相关文章

  1. Python Parameter Passing Note

    我刚刚开始学习Python, Python中的参数传递总是让我很困惑.我写了4个简单的Demo,帮助我理解Python的参数传递,希望对大家都能有所帮助. 0: def change(x): x = ...

  2. Default Parameter Values in Python

    Python’s handling of default parameter values is one of a few things that tends to trip up most new ...

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

  4. Go parameter passing

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

  5. 8. Object References, Mutability, and Recycling

    1. Variables Are Not Boxes # Think variables as sticky notes a = [1, 2, 3] b = a a.append(4) print b ...

  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函数参数默认值的陷阱和原理深究"

    本文将介绍使用mutable对象作为Python函数参数默认值潜在的危害,以及其实现原理和设计目的 本博客已经迁移至: http://cenalulu.github.io/ 本篇博文已经迁移,阅读全文 ...

  9. Python基础-函数(function)

    这里我们看看Python中函数定义的语法,函数的局部变量,函数的参数,Python中函数的形参可以有默认值,参数的传递是赋值操作,在函数调用时,可以对实参进行打包和解包  1,函数定义 关键字def引 ...

随机推荐

  1. RGB格式等比例缩放

    原理为:将原始图像的每个像素通过一个比例关系式映射到相应的位置. /* lrgb: input 24bits rgb buffer srgb: output 24bits rgb buffer wid ...

  2. Android中Intent传递类对象的方法一(Serializable)

    Activity之间通过Intent传递值,支持基本数据类型和String对象及它们的数组对象byte.byte[].char.char[].boolean.boolean[].short.short ...

  3. C++语言基础(18)-模板

    Java中的泛型编程可以极大的提升编程的效率,比如在android中查找一个控件的ID:标准写法为: TextView tv_text = (TextView)findViewById(R.id.tv ...

  4. 关于public class

    初学问题:“The public type movietestdive must be defined in its own file” 对于一个class里,只能出现一个public class(公 ...

  5. 自定义 Collection View 布局

    自定义 Collection View 布局 answer-huang 29 Mar 2014 分享文章 UICollectionView 在 iOS6 中第一次被引入,也是 UIKit 视图类中的一 ...

  6. CPU亲和力

    http://blog.chinaunix.net/uid-27714502-id-3515874.html http://www.tuicool.com/articles/I7NFzy http:/ ...

  7. js instanceof (2)

    instanceof运算符可以用来判断某个构造函数的prototype属性是否存在另外一个要检测对象的原型链上.实例一:普遍用法 A instanceof B :检测B.prototype是否存在于参 ...

  8. Cut the rope

    http://acm.nyist.net/JudgeOnline/problem.php?pid=651 描述We have a rope whose length is L. We will cut ...

  9. lucene学习-创建索引

    本文的lucene是基于lucene3.5版本. 使用lucene实现搜索引擎开发,核心的部分是建立索引和搜索.本节主要是记录创建索引部分的内容. 创建的索引结构如图所示. 创建索引的步骤分为以下几个 ...

  10. python3----字符串中的字符倒转

    方法一,使用[::-1]: s = 'python' print(s[::-1]) 方法二,使用reverse()方法: n = list(s) n.reverse() print(''.join(n ...