Objects and values

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,布布扣,bubuko.com

时间: 2024-10-10 02:26:08

Objects and values的相关文章

OC中的Values——and——Collections

值和集合 在OC中可以使用C中的基本数据类型. 可以在类的实现里面对这些基本数据类型的属性进行C中的:++ – += -= *=等操作. 如下所示: //---------接口声明----------- @interface XYZPerson : NSObject @property int age; -(void)gettingOlder; @end //----------接口实现----------- @implementation XYZPerson -(void)gettingOld

Python之路【第十七篇】:Django【进阶篇 】

Python之路[第十七篇]:Django[进阶篇 ] Model 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接数据库,并编写数据访问层代码 业务逻辑层去调用数据访问层执行数据库操作 import MySQLdb def GetList(sql): db = MySQLdb.connect(user='root', db='wupeiqidb', passwd='1234', host='localhost')

python运维开发(二十)----models操作、中间件、缓存、信号、分页

内容目录 select Form标签数据库操作 models操作F/Q models多对多表操作 Django中间件 缓存 信号 分页 select Form标签补充 在上一节中我们可以知道Form标签有两个作用,一个做用户提交数据验证,一个是生成HTML标签 在生成HTML标签中可以生成select标签,select的选项数据我们可以在数据库中查询读取到. class UserType(models.Model): caption = models.CharField(max_length=1

Django详解之models操作

D jango 模型是与数据库相关的,与数据库相关的代码一般写在 models.py 中,Django 支持 sqlite3, MySQL, PostgreSQL等数据库,只需要在settings.py中配置即可,不用更改models.py中的代码,丰富的API极大的方便了使用. 1.数据库的连接方式以及设置:在Django中默认使用的数据库类型是sqlite3,如果想要使用其他数据库就需要在settings中设置数据库的连接方式: # Database # https://docs.djang

Python Day19-20(Django基础)

一.Django基础 1.基本配置补充 可以用pycharm一键创建工程和APP 其他常用命令: python manage.py runserver 1.1.1.1:8000 python manage.py startapp appname python manage.py syncdb python manage.py makemigrations python manage.py migrate python manage.py createsuperuser 数据库配置 setting

Python学习笔记第二十周

目录: 一.ORM 1.查询补充 内容: 一.ORM 1.查询补充: 1.models.USR.objects.all().values('id','name') 这个语句相当于SQL语句中的select id,name from USER  ,表示可以单独取某几列的值,取得的值仍然是QuerySet,但是内部元素变为字典 2.models.USR.objects.all().value_list('id','name') 表示可以单独取某几列的值,取得的值仍然是QuerySet,但是内部元素变

Django基础

一.创建数据库 1.在APP下的models创建表,django可以自动创建表,不可以自动创建数据库 class userinfo(models.Model): username = models.CharField(max_length=50) password = models.CharField(max_length=50) 2.连接数据库,在settings.py下更改数据库的配置信息和应用的APP,Django项目建成后, 默认设置了使用SQLite数据库, 在my_blog/my_b

Django学习系列之ORM-QuerySetAPI

基本操作 # 增 models.Tb1.objects.create(c1='xx', c2='oo') #增加一条数据,可以接受字典类型数据 **kwargs obj = models.Tb1(c1='xx', c2='oo') obj.save() dic = {'c1':'xx','c2':'oo'} models.Tb1.objects.create(**dic) #Form的产出结果是一个字典,可以根据这个Form的字典和**直接在数据库创建数据 # 查 models.Tb1.obje

Python开发【第二十二篇】:Web框架之Django【进阶】

Python开发[第二十二篇]:Web框架之Django[进阶] 猛击这里:http://www.cnblogs.com/wupeiqi/articles/5246483.html 博客园 首页 新随笔 联系 订阅 管理 随笔-124  文章-127  评论-205 Python之路[第十七篇]:Django[进阶篇 ] Model 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接数据库,并编写数据访问层代码 业务逻