Python, difference between two ways for removing last elem of list
来源:互联网 发布:淘宝摄影技巧 编辑:程序博客网 时间:2024/05/16 19:46
For immutable variable, python is pass-by-value. Actually, python will create a new instance for immutable variable. But for mutable variable, python is pass-by-reference.
I found list = list[0:-1], python will create a new sublist, but for list.pop(), it will not.
Here is an example.
Example 1
a = [1,2,3]print id(a)def func(lst): print id(lst) lst = lst[0:-1] pritn lst, id(lst)func(a)print a, id(a)
The output is
41478515644147851564[1, 2] 4147852172[1, 2, 3] 4147851564
In this example, id() function is used to identify object. It is an integer which is guaranteed to be unique and constant for this object. We could think of it as the memory address.
We can find, in this example, the lst = lst[0:-1] give rise to the change of address of lst. The original list a is not affected.
Example 2
a = [1,2,3]print id(a)def func(lst): print id(lst) lst.pop() print lst, id(lst)func(a)print a, id(a)
Output is
41479334844147933484[1, 2] 4147933484[1, 2] 4147933484
We notice that, for list.pop(), python will not alter the address of list.
- Python, difference between two ways for removing last elem of list
- difference between str & list in python
- Calculate difference between two dates (number of days) in C#?
- one mail for difference between LOCAL_LDLIBS and LOCAL_SHARED_LIBRARIES of Android
- Maximum Difference Between Two Elements
- difference between slist and list.
- Difference Between List and Tuples
- two ways of multiThread
- What the difference between these two command!
- Two ways of using Iterator
- Two Smallest Difference of Ad
- Difference between forward and include of RequestDispatcher
- difference between data() and c_str() of basic_string
- [java] difference between Abstract Classes of Interface
- Difference between OnPaint and OnDraw of MFC
- What is the difference between urllib and urllib2 modules of Python?
- Difference between Python 2 and Python 3
- Excuses for last two days...
- Java 自定义实现 LRU 缓存算法
- 《剑指Offer》面试题:替换空格
- 两个div块之间的缺口问题,border与z-index实现
- Disply和Visibility的区别
- Java日志终极指南
- Python, difference between two ways for removing last elem of list
- STL源码剖析 [容器](六)[stl_queue.h]
- JVM性能优化,Java的伸缩性
- Eclipse方便的管理Tomcat (Eclipse + WTP + Tomcat 配置 )
- Solr使用初探——SolrJ的使用
- i++和++i的区别
- solrJ使用样例
- Android Studio 实现真机截图和短视频录制
- JProfiler 入门教程(提供下载PDF)