More about Parameter Passing in Python(Mainly about list)
我之前写了一篇关于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)的更多相关文章
- Python Parameter Passing Note
我刚刚开始学习Python, Python中的参数传递总是让我很困惑.我写了4个简单的Demo,帮助我理解Python的参数传递,希望对大家都能有所帮助. 0: def change(x): x = ...
- 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 ...
- 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 ...
- Go parameter passing
package main import ( "fmt" ) func main() { fmt.Println("Hello, playground") var ...
- 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 ...
- [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函数参数默认值的陷阱和原理深究"
本文将介绍使用mutable对象作为Python函数参数默认值潜在的危害,以及其实现原理和设计目的 本博客已经迁移至: http://cenalulu.github.io/ 本篇博文已经迁移,阅读全文 ...
- Python基础-函数(function)
这里我们看看Python中函数定义的语法,函数的局部变量,函数的参数,Python中函数的形参可以有默认值,参数的传递是赋值操作,在函数调用时,可以对实参进行打包和解包 1,函数定义 关键字def引 ...
随机推荐
- SpringBoot+MybatisPlus(多数据源和主从分离)
简介 dynamic-datasource-spring-boot-starter 基于 springBoot2.0. 它适用于读写分离,一主多从的环境. 主数据库使用 INSERT UPDATE D ...
- iOS一些基础面试题
Part One 别人问你你都感觉这尼玛说啥的基础面试题 1.UIWindow和UIView和 CALayer 的联系和区别? 答:UIView是视图的基类,UIViewController是视图控制 ...
- error: memcached support requires ZLIB. Use --with-zlib-dir=<DIR> to specify the prefix where ZLIB
yum install zlib-devel
- android开发的权限获取 (转载的)
访问登记属性 android.permission.ACCESS_CHECKIN_PROPERTIES ,读取或写入登记check-in数据库属性表的权限 获取错略位置 android.permiss ...
- Linux Linux系统管理命令
df 检测文件系统的磁盘空间占用和空余情况 --用于检测文件系统的磁盘空间占用和空余情况,可以显示所有文件系统对接点和磁盘块的使用情况 --df [参数] --常用参数及含义 "-a&quo ...
- python 装饰器 (多个参数的函数,带参数的装饰器)
最简单的模板是这样的 #-*-coding:utf-8-*- def outer(func): def inner(): print 'before' func() print 'after' # r ...
- 基于python的七种经典排序算法(转)
一.排序的基本概念和分类 所谓排序,就是使一串记录,按照其中的某个或某些关键字的大小,递增或递减的排列起来的操作.排序算法,就是如何使得记录按照要求排列的方法. 排序的稳定性:经过某种排序后,如果两个 ...
- Quartz定时任务学习(三)属性文件和jar
以下是我在应用的的一个基本配置: #---------调度器属性----------------org.quartz.scheduler.instanceName = TestSchedulerorg ...
- Unity3D学习笔记——NGUI之UIGrid
UIGrid:这个组件可以轻松的让你排列你的组件,并且在运行或是编辑的时候都可以. 效果图如下: 一:使用步骤 1.选择一个panel然后右键Create——Grid 2.为Grid创建几个子Spri ...
- C++ 虚析构(virtual destructor)原理
注意:本文仅为个人理解,可能有误! 先看一段代码: #include <iostream> using namespace std; class CBase{ public: CBase( ...