关于Python中数据对象的可变性
先贴上Python官网中对数据模型描述的几段话。(在python官网的 语言参考>>数据模型 那部分)
Every object has an identity, a type and a value. An object’s identity never changes once it has been created; you may think of it as the object’s address in memory. The ‘is‘ operator compares the identity of two objects; the id() function returns an integer representing its identity.
An object’s type determines the operations that the object supports (e.g., “does it have a length?”) and also defines the possible values for objects of that type. The type() function returns an object’s type (which is an object itself). Like its identity, an object’s type is also unchangeable.
The value of some objects can change. Objects whose value can change are said to be mutable; objects whose value is unchangeable once they are created are called immutable. (The value of an immutable container object that contains a reference to a mutable object can change when the latter’s value is changed; however the container is still considered immutable, because the collection of objects it contains cannot be changed. So, immutability is not strictly the same as having an unchangeable value, it is more subtle.) An object’s mutability is determined by its type; for instance, numbers, strings and tuples are immutable, while dictionaries and lists are mutable.
Some objects contain references to other objects; these are called containers. Examples of containers are tuples, lists and dictionaries. The references are part of a container’s value. In most cases, when we talk about the value of a container, we imply the values, not the identities of the contained objects; however, when we talk about the mutability of a container, only the identities of the immediately contained objects are implied. So, if an immutable container (like a tuple) contains a reference to a mutable object, its value changes if that mutable object is changed.
每个对象有: identity, type, value; 其中 identity 和 type 总是 不可变 的。
对于对象的值来说,有的可变,有的不可变。
例如:numbers, strings and tuples 是 不可变 的; dictionaries and lists 是 可变 的。
有些对象包含其它对象的引用,这些对象叫做容器(containers), 例如 tuples, lists and dictionaries.
这些引用是一个容器的 value 的一部分。通常,我们说到一个容器的 value 的时候,我们暗指的是该容器所包含的对象的 value, 而不是对象的 identities。
然而当我们说到一个容器的可变性的时候,我们暗指的是该容器直接包含的对象的 identity.
因此,如果一个不可变容器(比如 tuple)包含一个到可变对象的引用的时候,如果那个可变对象改变的话它的值就改变了。
以上算是对英文原文的选择性翻译,下面举个栗子:
以为理解了,真要写出来让别人能看懂的时候反倒自己又迷糊了,待加,待加
关于Python中数据对象的可变性的更多相关文章
- python中拷贝对象的区别
一.赋值.引用 在python中赋值语句总是建立对象的引用值,而不是复制对象.因此,python变量更像是指针,而不是数据存储区域 这点和大多数语音类似吧,比如C++.Java等 1.先看个例子: v ...
- PyObject and PyTypeObject - Python 中的 '对象' 们
1 PyObject, PyTypeObject - Python 中的 '对象' 们 '一切皆对象' - 这是 Python 的学习和使用者们最最常听到一句, 可谓 博大精深 - '勃大精深'. ' ...
- python中生成器对象和return 还有循环的区别
python中生成器对象和return 还有循环的区别 在python中存在这么一个关键字yield,这个关键字在项目中经常被用到,比如我写一个函数不想它只返回一次就结束那我们就不能用return,因 ...
- Python中Json对象处理的jsonpath-rw
这两天在写一个爬虫,需要从网站返回的json数据提取一些有用的数据. 向url发起请求,返回的是response,在python3中,response.content是二进制bytes类型的,需要用d ...
- python中的对象(三)
一.python对象 python使用对象模型来存储数据.构造任何类型的值都是一个对象. 所有python对象都拥有三个特性:身份.类型.值 身份:每个对象都有一个唯一的身份标识自己,任何对象的身份可 ...
- python中的对象
一.python对象 python使用对象模型来存储数据.构造任何类型的值都是一个对象. 所有python对象都拥有三个特性:身份.类型.值 身份:每个对象都有一个唯一的身份标识自己,任何对象的身份可 ...
- python中数据类型转换
python中list和str互转 1.list转str 假设有一个名为test_list的list,转换后的str名为test_str 则转换方法: test_str = "" ...
- [19/10/14-星期一] Python中的对象和类
一.面向对象 ## 什么是对象? - 对象是内存中专门用来存储数据的一块区域. - 对象中可以存放各种数据(比如:数字.布尔值.代码) - 对象由三部分组成: 1.对象的标识(id) 2.对象的类型( ...
- Python中数据的保存和读取
在科学计算的过程中,往往需要保存一些数据,也经常需要把保存的这些数据加载到程序中,在 Matlab 中我们可以用 save 和 lood 函数很方便的实现.类似的在 Python 中,我们可以用 nu ...
随机推荐
- [LeetCode]题解(python):081 - Search in Rotated Sorted Array II
题目来源 https://leetcode.com/problems/search-in-rotated-sorted-array-ii/ Follow up for "Search in ...
- empty()函数经典详解
<?php /** * 当var不存在,返回TRUE; * 当var存在,并且是一个非空非零的值(真值)时返回 FALSE 否则返回 TRUE . * 以下的东西被认为是空的: * * 1.&q ...
- shell && 和 || 的短路使用
shell && 和 || 的短路使用 && 和 || 在 shell 中分别表示 and 和 or,和其它语言类似,这两个操作有短路效应.也就是说,当判断式已经确定时 ...
- ubuntu虚拟机安装
安装VMware Workstation 下载ubuntu镜像: http://www.ubuntu.com/download/ git for Windows下载: http://msysgit.g ...
- ArcBruTile 0.2.2
在ArcGIS中加载OpenStreetMap和Google.Bing的影像是不是很酷?用ArcBruTile 0.2.2可以实现.安装注册步骤如下.
- Vue.2.0.5-模板语法
Vue.js 使用了基于 HTML 的模版语法,允许开发者声明式地将 DOM 绑定至底层 Vue 实例的数据.所有 Vue.js 的模板都是合法的 HTML ,所以能被遵循规范的浏览器和 HTML 解 ...
- 解决: Fail to create empty document
做 Programming Windows with MFC 2nd 的例子 MyWord 的时候. 发现启动的时候总是报错: Fail to create empty document. 搜索了一下 ...
- instancetype
- How to use JDBC-Authentication of Spring Boot/Spring Security with Flyway
java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.conte ...
- bash变量操作
1.条件变量替换: Bash Shell可以进行变量的条件替换,既只有某种条件发生时才进行替换,替换 条件放在{}中. (1) ${value:-word} 当变量未定义或者值为空时,返回值为word ...