我之前写了一篇关于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. 实现html锚点的两种方式

    1,a标签+name属性. 2,使用标签的id属性:

  2. ssh-keygen配合ssh_config免密码登录VPS

    ssh-keygen配合ssh_config免密码登录VPS Posted by fiture / 2012年12月29日 / 「Ubuntu」「分享」 用过终端登录远程服务器或者VPS的童鞋都用过类 ...

  3. shell脚本中处理 路径中中文和空格方法

    OLDIFS=$IFS IFS=$'\n' #存放路径的变量在引用时都使用双引号括起来 "$VAR_PATH" #路径拼接时后续的旧不需要加双引号了"$VAR_PATH& ...

  4. c++ about SLL(Static-Link Library) and DLL(Dynamic-Link Library)

    First thing first, Wiki: http://en.wikipedia.org/wiki/Dynamic-link_library http://en.wikipedia.org/w ...

  5. 内核交互--procfs

    文档介绍:http://lxr.linux.no/linux+v2.6.37/Documentation/filesystems/proc.txt以下内容抄录linux设备驱动开发详解-宋宝华在/pr ...

  6. linux虚拟文件系统vfs

    linux可以挂载不同的文件系统(EXT2,FAT,NTFS),用同一的样式呈现给用户,读写操作用起来都一样,这是怎样做到的呢? linux内核在各种不同的文件系统格式上做了一个抽象层,使得文件.目录 ...

  7. Swift 开源 Linux Ubuntu Install

    Swift 开源了,它现在变成跨平台的了,开源后的 Swift 不止能运行在 MAC 和 iOS 平台,现在也可以运行在 Linux 平台了.swift.org 网站上面提供了在 Linux 上面安装 ...

  8. apple touch icon 大小总结

    <!-- For Chrome for Android: --> <link rel="icon" sizes="192x192" href= ...

  9. 通过Bag一对多映射示例(使用xml文件)

    如果持久化类具有包含实体引用的列表对象,则需要使用一对多关联来映射列表元素. 我们可以通过列表(list)或包(bag)来映射这个列表对象. 请注意,bag不是基于索引的,而list是基于索引的. 在 ...

  10. shell学习五十八天----/proc文件系统

    /proc文件系统 前言:linux中的/proc文件系统,由一组文件夹和文件组成,挂载(mount)与/proc文件夹下. /proc文件系统是一种虚拟文件系统,以文件系统文件夹和文件形式,提供一个 ...