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的更多相关文章

  1. 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 ...

  2. JavaScript Objects in Detail

    JavaScript’s core—most often used and most fundamental—data type is the Object data type. JavaScript ...

  3. Processing Images

    https://developer.apple.com/library/content/documentation/GraphicsImaging/Conceptual/CoreImaging/ci_ ...

  4. JavaScript Patterns 3.1 Object Literal

    Basic concept Values can be properties: primitives or other objects methods: functions User-defined ...

  5. C# - String与StringBuilder

    A String object is called immutable (read-only), because its value cannot be modified after it has b ...

  6. 《Think Python》第15章学习笔记

    目录 <Think Python>第15章学习笔记 15.1 程序员定义的类型(Programmer-defined types) 15.2 属性(Attributes) 15.3 矩形( ...

  7. JS对象深入剖析

    对象概述 Objects are mutable keyed collections.  An object is a container of properties, where a propert ...

  8. python sentence

    1.while for 增加了循环正常结束后执行的else代码块. 2.Objects are mutable 3.import copy p1 = Point() p2=copy.copy(p1) ...

  9. Why are C# structs immutable?

    Compiler Error CS1612 Cannot modify the return value of 'expression' because it is not a variable cl ...

随机推荐

  1. BZOJ 1264: [AHOI2006]基因匹配Match 树状数组+DP

    1264: [AHOI2006]基因匹配Match Description 基因匹配(match) 卡卡昨天晚上做梦梦见他和可可来到了另外一个星球,这个星球上生物的DNA序列由无数种碱基排列而成(地球 ...

  2. httpClient模拟登陆校内某系统

    package com.huowolf; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpE ...

  3. ubuntu12.04下NFS链接开发板并测试交叉编译的第一个应用

    思路:配置网络->安装NFS->配置NFS->挂载NFS服务->Down文件执行.Okay lets go! 配置网络: 在配置网络之前,首先咱得搞定与开发板的交互工作,那么这 ...

  4. [NOIP2017] 逛公园 解题报告(DP)

    我很不想说 在我的AC代码上我打了表,但实在没有办法了.莫名的8,9个点RE.然而即便是打表...也花了我很久. 这大概是NOIP2017最难的题了,为了让不懂的人更容易理解,这篇题解会比较详细 我的 ...

  5. _itoa进制转换

    #define _CRT_SECURE_NO_WARNINGS #include <stdlib.h> #include <stdio.h> void main() { int ...

  6. Struts2.3.16.1+Hibernate4.3.4+Spring4.0.2 框架整合(转)

    原文  http://blog.csdn.net/songanling/article/details/22454973 最新版Struts2+Hibernate+Spring整合     目前为止三 ...

  7. 50个极好的bootstrap框架

    转自:http://sudasuta.com/bootstrap-admin-templates.html  https://www.cnblogs.com/sanhao/p/9184323.html ...

  8. VBA 中Dim含义

    楼主是个初学者,在应用vba时遇到了dim方面的问题,查了很多资料后想把关于dim的这点儿知识简单整理出来 首先,从我遇到的问题作为切入点吧, (不得不承认我遇到的错误是很低级的) 具体的情境就不还原 ...

  9. .Net基础杂记

    1.面向对象程序思想 面向对象是程序开发的一种机制,特征为封装.继承.多态.以面向对象方式编写程序时,将复杂的项目抽象为多个对象互相协作的模型,然后编写模型结构,声明或实现类型的成员,即描述对象的特征 ...

  10. [洛谷P1892][codevs2597]团伙

    题目大意:有n个强盗,他们有这样的关系:1.朋友的朋友是朋友:2.敌人的敌人是朋友. 两个人是朋友,则他们在一个团伙中,是敌人则在不同团伙中. 现在给出一些朋友或敌人的关系,问最多有多少团伙.输入保证 ...