Objects are mutable
We can change the state of an object by making an assignment to one of its attributes. For example, to change the size of a rectangle without changing its position, you can modify the values of width and height:
box.width = box.width + 50
box.height = box.height + 100
You can also write functions that modify objects. For example, grow_rectangle takes a Rectangle object and two numbers, dwidth and dheight, and adds the numbers to the width and height of the rectangle:
def grow_rectangle(rect,dwidth,dheight):
rect.width += dwidth
rect.height += dheight
Here is an example that demonstrates the effect:

Inside the function, rect is an alias for box, so if the function modifies rect, box changes.
from Thinking in Python
Objects are mutable的更多相关文章
- Think Python - Chapter 15 - Classes and objects
15.1 User-defined typesWe have used many of Python’s built-in types; now we are going to define a ne ...
- JavaScript Objects in Detail
JavaScript’s core—most often used and most fundamental—data type is the Object data type. JavaScript ...
- Processing Images
https://developer.apple.com/library/content/documentation/GraphicsImaging/Conceptual/CoreImaging/ci_ ...
- JavaScript Patterns 3.1 Object Literal
Basic concept Values can be properties: primitives or other objects methods: functions User-defined ...
- C# - String与StringBuilder
A String object is called immutable (read-only), because its value cannot be modified after it has b ...
- 《Think Python》第15章学习笔记
目录 <Think Python>第15章学习笔记 15.1 程序员定义的类型(Programmer-defined types) 15.2 属性(Attributes) 15.3 矩形( ...
- JS对象深入剖析
对象概述 Objects are mutable keyed collections. An object is a container of properties, where a propert ...
- python sentence
1.while for 增加了循环正常结束后执行的else代码块. 2.Objects are mutable 3.import copy p1 = Point() p2=copy.copy(p1) ...
- Why are C# structs immutable?
Compiler Error CS1612 Cannot modify the return value of 'expression' because it is not a variable cl ...
随机推荐
- HDU 4310 Contest 2
贪心,注意排序条件. #include <iostream> #include <cstdio> #include <algorithm> using namesp ...
- SEO分享:关于SEO的十个问题
想写的幽默一点,幽默细胞太少,想写的专业一点,又不够专业,结果就出现了这篇不伦不类的怪文,望海涵! 导读:前面写过一篇类似的文章,受到的评价好坏都有吧.有人说讲的没有实质性的内容,有些人抱怨回答的太过 ...
- Android sdCard路径问题
一,获取Android设备的全部存储设备,这里边肯定有一个能用的 StorageManager sm = (StorageManager) context.getSystemService(Conte ...
- 110个经常使用Oracle函数总结
1. ASCII 返回与指定的字符相应的十进制数; SQL> select ascii(A) A,ascii(a) a,ascii(0) zero,ascii( ) space from dua ...
- elasticsearch如何安全重启
elasticsearch如何安全重启节点 问题: elasticsearch集群,有时候可能需要修改配置,增加硬盘,扩展内存等操作,需要对节点进行维护升级.但是业务不能停,如果直接kill掉节 点, ...
- Service Mesh(服务网格)
Service Mesh(服务网格) 什么是Service Mesh(服务网格)Service mesh 又译作 "服务网格",作为服务间通信的基础设施层.Buoyant 公司的 ...
- POJ 1852 Ants O(n)
题目: 思路:蚂蚁相碰和不相碰的情况是一样的,相当于交换位置继续走. 代码: #include <iostream> #include <cstdio> #include &l ...
- MySQL学习(六)——自定义连接池
1.连接池概念 用池来管理Connection,这样可以重复使用Connection.有了池,我们就不用自己来创建Connection,而是通过池来获取Connection对象.当使用完Connect ...
- Python3.x和Python2.x的区别(转存参考)
http://www.360doc.com/content/14/0619/23/16740871_388198818.shtml 这个星期开始学习Python了,因为看的书都是基于Python2.x ...
- SpringCloud学习笔记(17)----Spring Cloud Netflix之服务网关Zuul的使用
1. 什么时候Zuul? Zuul是一个基于jvm路由和服务端的负载均衡器,在云平台上提供动态路由,监控,弹性,安全等边缘服务的框架. 路由功能:相当于nginx的反向代理 比如: / 可能需要映射到 ...