我之前写了一篇关于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. Atitit..net clr il指令集 以及指令分类  与指令详细说明

    Atitit..net clr il指令集 以及指令分类  与指令详细说明 1.1. .NET CLR 和 Java VM 都是堆叠式虚拟机器(Stack-Based VM), 1 1.2. 查看工具 ...

  2. C语言变长參数的认识以及宏实现

    1.认识 变长參数是C语言的特殊參数形式.比如例如以下函数声明: int printf(const char *format, ....); 如此的声明表明,printf函数除了第一个參数类型为con ...

  3. C语言基础(20)-文件操作(fopen,getc,fclose)

    一.文件操作 读文件的顺序: 1.先打开文件 2.读写文件 3.关闭文件 1.1 fopen FILE *fopen( const char *path, const char *mode ); 函数 ...

  4. gcc -M -MM -MQ -MF -MT -MD

    静态模式规则对一个较大工程的管理非常有用.它可以对整个工程的同一类文件的重建规则进行一次定义,而实现对整个工程中此类文件指定相同的重建规则.比如,可以用来描述整个工程中所有的.o 文件的依赖规则和编译 ...

  5. vm中安装ubuntu16

    安装完成后,开机出现 smbus host controller not enabled 上网照着网友的建议: 在grub界面选择高级选项 root shell 进入到shell之后 mount -o ...

  6. python模块学习之re

    正则表达式本质就是表示某种规则的一串字符. 匹配的规则叫做模式(pattern),模式作用于对象. 模式和对象可以是Unicode或者字节,但是,不能够混用,比如:模式为Unicode,对象为字节,像 ...

  7. deepin linux下python安装mysqldb

    ` sudo pip search MySQL-python `

  8. CPU亲和力

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

  9. Call to a member function cellExists() on a non-object /phpexcel/Classes/PHPExcel/Calculation.php on line 3241

    PHP Fatal error: Call to a member function cellExists() on a non-object in /home/edata/eframework/we ...

  10. Web services 把 Web 应用程序提升到了另外一个层面

    通过使用 Web services,您的应用程序可向全世界发布功能或消息. Web services 使用 XML 来编解码数据,并使用 SOAP 借由开放的协议来传输数据. 通过 Web servi ...