我刚刚开始学习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. Unix系统编程(六)write系统调用

    write系统调用将数据写入一个打开的文件. ssize_t write(int fd, void *buffer, size_t count); write调用的参数含义与read调用相类似.buf ...

  2. ios-A+B经典问题

    // // main.m // a+b // #import <Foundation/Foundation.h> #import "Calcultor.h" int m ...

  3. php 扩展模块添加

    1. 新增安装扩展模块的位置 [root@node_22 ~]# ls /usr/local/php7/lib/php/extensions/no-debug-non-zts-20151012/ op ...

  4. is_array

    is_array (PHP 4, PHP 5) is_array — 检测变量是否是数组

  5. c#上传大文件方法

    客户端代码: /// <summary> /// 将本地文件上传到指定的服务器(HttpWebRequest方法) /// </summary> /// <param n ...

  6. (转)java反编译i++和++i问题

    转自:http://blog.csdn.net/junsure2012/article/details/7099222 java字节码指令集:http://www.jb51.net/article/3 ...

  7. python 国内镜像

    pipy国内镜像目前有: http://pypi.douban.com/  豆瓣 http://pypi.hustunique.com/  华中理工大学 http://pypi.sdutlinux.o ...

  8. Java每日一题

    1.(单选题)What will be printed when you execute the following code? class C { C() { System.out.print(&q ...

  9. 【转】 JS实现HTML标签转义及反转义

    原文地址:http://blog.600km.xyz/2015/12/15/js-encode-html-tags/ 简单说一下业务场景,前台用户通过input输入内容,在离开焦点时,将内容在div中 ...

  10. Java日志记录工具SLF4J介绍

    SLF4J是什么 SLF4J是一个包装类,典型的facade模式的工具,对用户呈现统一的操作方式,兼容各种主流的日志记录框架,典型的有log4j/jdk logging/nop/simple/jaka ...