码迷,mamicode.com
首页 > 编程语言 > 详细

[Python] Understand Mutable vs. Immutable objects in Python

时间:2017-12-09 19:32:54      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:foo   less   mtu   you   change   this   diff   color   min   

In this lesson, you will learn what mutable and immutable objects are, and the difference between them. This understanding will help you determine when objects can be modified in place, and when new objects must be created.

 

List is mutable, which means everytime it returns the same id whether or not you have changed it:

foo = []
id(foo) // same
foo.append(3)
id(foo) // same

 

Immtuable such as string:

str = "Hello"
id(str) // not the same
str = "World"
id(str) // not the same

 

[Python] Understand Mutable vs. Immutable objects in Python

标签:foo   less   mtu   you   change   this   diff   color   min   

原文地址:http://www.cnblogs.com/Answer1215/p/8012188.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!