我刚刚开始学习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. pycharm 社区版

    这个是免费版.到3.1.13 http://pan.baidu.com/s/1bnvPdtt 没用过idea的商业版,不知道为什么它的更新那么蛋疼,不能增量更新,每次都得手动下载完整的压缩包.

  2. PHP——文本编辑器

    简单的代码演示 详细文件在文件目录里 <!doctype html> <html> <head> <meta charset="utf-8" ...

  3. Mysql root密码忘记的解决办法

    Windows 版本: 1.打开安装目录下的my.ini 找到 [mysqld] 在下面加入 skip-grant-tables 2. 重启mysql服务 3.打开命令行 依次输入 USE mysql ...

  4. Visual Studio中“后期生成事件命令行” 中使用XCopy命令

    将程序所依赖的动态库与其他依赖文件做了分类,使用XCopy命令自动生成相应的目录结构. set source="$(TargetDir)" set output="$(S ...

  5. SVN版本库的备份、还原、移植(初级篇、中级篇和高级篇)

    版本库数据的移植:svnadmin dump.svnadmin load 导出: $svnlook youngest myrepos //查看到目前为止最新的版本号 $svnadmin dump my ...

  6. json中的日期格式转换(扩展new date()显示格式)

    在java  spring mvc 开发过程中,通过json 格式,向前端传递数据,日期格式发生了转变, 在前台数据展示时,要进行一定格式的转换才能正常显示: 我在开发中使用了easy ui 和my ...

  7. 嵌入式驱动开发之spi---spi串口通信调试

    一. 概念 SPI是 Serial Peripheral Interface(串型外部接口)的缩写.SPI接口有4根PIN脚,分别是:          * SPICLK     : 用于传输数据的同 ...

  8. mac 安装memcache扩展问题

    执行php -i 报错: Warning: PHP Startup: memcached: Unable to initialize module Module compiled with build ...

  9. c++ wchar_t

    ·C语言相关 对应于char, C语言中也有宽字符内型wchar_t.wchar_t被定义为: typedef unsigned short wchar_t ;显然它是16位的.wchar_t类型的常 ...

  10. iOS 去掉navgationbar 底部线条

    [[UINavigationBar appearance] setBarStyle:UIBarStyleBlackTranslucent]; [[UINavigationBar appearance] ...