If we execute these assignment statements:

We know that a and b both refer to a string, but we don’t know whether they refer to the same string. To check whether two variables refer to the same object, you can use the is operator. And the result shows in this example, Python only created one string object, and both a and b refer to it.

In contrast, when you create two lists, you get two objects:

In this case we would say that the two lists are equivalent, because they have the same elements, but not identical, because they are not the same object. If two objects are identical, they are also equivalent, but if they are equivalent, they are not necessarily identical. Until now, we have been using ‘Object’ and ‘value’ interchangeably, but it is more precise to say that an object has a value. If you execute a = [1,2,3], a refers to a list object whose value is a particular sequence of elements. If another list has the same elements, we would say it has the same value.

Aliasing

If a refers to an object and you assign b = a, then both variables refer to the same object. For example if you execute:

The association of a variable with an object is called reference. In this example, there are two references to the same object. An object with more than one reference has, in some sense, more than one name, so we say that the object is aliased. If the aliased object is mutable, changes made with one alias affect the other:

Although this behaviour can be useful, it is sometimes unexpected or undesirable. In general, it is safer to avoid aliasing when you are working with mutable objects. For immutable objects like strings, aliasing is not as much of a problem. For example string, it almost never makes a difference whether a and b refer to the same string or not.

from Thinking in Python

Objects and values的更多相关文章

  1. djano modles values+ajax实现无页面刷新更新数据

    做项目的过程中想通过不刷新页面的方式来进行页面数据刷新,开始使用http://www.cnblogs.com/ianduin/p/7761400.html方式将查询结果数据进行序列化.发现可以行,但是 ...

  2. Django 二——models(admin、ORM),一对一、一对多、多对多操作,all、values、value_list的对比

    内容概要 1.关系对象映射ORM 2.admin的配置(选修) 3.all().values().value_list()的对比 4.数据库操作(一对一.一对多.多对多) 5.HttpResponse ...

  3. Django

    一.Django 简介 Django 是一个由 Python 写成的开放源代码的 Web 应用框架.它最初是被开发来用于管理劳伦斯出版集团旗下的一些以新闻内容为主的网站的,即是 CMS(内容管理系统) ...

  4. Python攻关之Django(一)

    课程简介: Django流程介绍 Django url Django view Django models Django template Django form Django admin (后台数据 ...

  5. django之一些feature

    前端之django一些feature 本节内容 cookie session 跨站请求保护 分页 序列化 model模块 CBV和FBV 模板渲染对象 1. cookie cookie 是一种发送到客 ...

  6. python框架之django

    python框架之django 本节内容 web框架 mvc和mtv模式 django流程和命令 django URL django views django temple django models ...

  7. Django基础之安装配置

    安装配置 一 MVC和MTV模式 著名的MVC模式:所谓MVC就是把web应用分为模型(M),控制器(C),视图(V)三层:他们之间以一种插件似的,松耦合的方式连接在一起. 模型负责业务对象与数据库的 ...

  8. 进击的Python【第二十一章】

    s14day21 上节内容回顾: 1.请求周期 url> 路由 > 函数或类 > 返回字符串或者模板语言? Form表单提交: 提交 -> url > 函数或类中的方法 ...

  9. 八月22日,django知识点总结:

    八月22日,知识点总结: python manage.py makemigrations python manage.py migrate unique=true是指这个字段的值在这张表里不能重复,所 ...

随机推荐

  1. 关于Segmentation fault错误

    今天敲代码时候出现了Segmentation fault,在网上查了一些资料,基本上的原因是.非法的内存訪问. 比如数组的越界,在循环操作时循环变量的控制问题,也有字符串拷贝时长度溢出,指针指向了非法 ...

  2. [Angular] Handle HTTP Errors in Angular with HttpErrorResponse interface

    When communicating with some backend API, data travels over the network using the HTTP protocol. As ...

  3. Hadoop 2.2.0和HBase-0.98 安装snappy

    1.安装须要的依赖包及软件 须要安装的依赖包有: gcc.c++. autoconf.automake.libtool 须要安装的配套软件有: Java6.Maven 关于上面的依赖包,假设在ubun ...

  4. uva 10641 (来当雷锋的这回....)

    #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using ...

  5. 实现一个类似360的button

    通过改写一个buttonst类,实现360效果的button. 主要可以完成:frame,hover,face效果,并且支持menu,tooltips 1)派生新的类QButton.添加虚函数,设置自 ...

  6. mount ntfs 失败解决办法

    在双系统中,ntfs可能会应为windows的缓存而挂载失败.可用下面命令修复. Use ntfsfix in the terminal, even if you can't access Windo ...

  7. 关于Tomcat的启动

    1.Tomcat分为安装版和解压版. 2.在Tomcat的解压版的bin路径下启动startup.bat的时候,如果没有启动成功,请检查是否设置了JAVA_HOME 3.建议不要在环境变量里面设置CA ...

  8. VC++ LPARAMA 转换成CString

    如果是SendMessage发送的CString CString s = "xxxxxx";SendMessage(hWnd,WM_XXXX,NULL,(LPARAM)&s ...

  9. uva 11584 - 字符串 dp

    题目链接 一个长度1000的字符串最少划分为几个回文字符串 ---------------------------------------------------------------------- ...

  10. Activiti 23张表及7大服务详解

    7大服务介绍 服务名称 描述 RepositoryService Activiti 中每一个不同版本的业务流程的定义都需要使用一些定义文件,部署文件和支持数据 ( 例如 BPMN2.0 XML 文件, ...