我刚刚开始学习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. (译)Getting Started——1.1.1 Start Developing IOS Today(开始IOS开发)

    安装       本课程对于创建运行在iPad.iPhone和iPod触摸屏上的应用来说,是一个完美的起点.该向导的四个板块可以作为构建你第一个应用的简单向导——内容包括了你需要使用的工具,主要的理念 ...

  2. mysql数据库表修改某一列的类型

    下面列出:1.增加一个字段alter table user add COLUMN new1 VARCHAR(20) DEFAULT NULL; //增加一个字段,默认为空alter table use ...

  3. busybox中的inittab解析

    init进程是由内核启动的第一个(也是唯一一个)用户进程(进程ID为1),是所有进程的祖先.然后init进程根据配置文件决定启动哪些程序,init是后续所有进程的发起者. 用busybox制作的文件系 ...

  4. Tomcat服务器的默认端口是多少?怎样修改tomcat的端口?

    Tomcat服务器的默认端口是多少?怎样修改tomcat的端口? 解答:默认端口为8080,可以通过service.xml的Connector元素的port属性来修改端口.

  5. 消息队列ipc的一些设置

    Linux IPC 参数设定- 命令方式: echo 80 > /proc/sys/vm/overcommit_ratio, etc MSGMNB 每个消息队列的最大字节限制. MSGMNI 整 ...

  6. WPF数据验证(4)——响应与获取验证错误

    1780 前面的示例中,有关用户接受到错误的唯一指示是在违反规则的文本框周围的红色轮廓.为了提供更多信息,可以处理 Error 事件,但存储或清除错误时会引发该事件,但前提是必须确保已将 Bindin ...

  7. python bottle学习(三)动态路由配置(通配符)

    from bottle import (run, route, get, post, default_app, Bottle) @route('/', method='GET') @route('/i ...

  8. Yolo+Windows 配置(详细版)

    一.配置环境  VS2013+显卡GtX1080ti+CUDA7.5+Opencv3.1.0+pthread pthread:ftp://sourceware.org/pub/pthreads-win ...

  9. poj3372

    Candy Distribution Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5868   Accepted: 327 ...

  10. Java一些七七八八的配置

    mysql驱动配置连接eclipse和控制台(cmd)(JAVA) https://blog.csdn.net/u013000747/article/details/55510671 BeanUtil ...