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. &lt;pre&gt;标签

     <pre>标签最主要的认识就是预格式化文本,被包围在 pre 元素中的文本一般会保留空格和换行符.而文本也会呈现为等宽字体.经经常使用于在网页中显示计算机源码. 1.格式化文本举例 &l ...

  2. 110_leetcode_Best Time to Buy and sell Stock II

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  3. nyoj 585 取石子(六) 【Nim】

    取石子(六) 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描写叙述 近期TopCoder的PIAOYI和HRDV非常无聊,于是就想了一个游戏,游戏是这种:有n堆石子,两个人 ...

  4. rails数据库操作rake db一点心得

    问题描述,对于很多的新手rails lover来说,搞定db是件头疼的事情,当建立了一个model,测试了半天发现我草列名写错了,再过一会儿发现association里面竟然没有xxx_id,这下子s ...

  5. [hihocoder #1384] Genius ACM 解题报告(倍增)

    题目链接:http://hihocoder.com/problemset/problem/1384 题目大意: 给定一个整数 M,对于任意一个整数集合 S,定义“校验值”如下: 从集合 S 中取出 M ...

  6. mybatis使用注解替代xml配置,动态生成Sql

    mybatis使用注解替代xml配置时,遇到判断条件是否为null或者为空时,@Select很难搞定,不知道怎么办? mybatis3中增加了使用注解来配置Mapper的新特性,使用 SelectPr ...

  7. Pharmaceutical的同学们都看过来,关于补码运算的复习相关内容

    虽然是全英文的课程,这次总结内容不用英文了. 一般在计算机原理中,对两个操作数进行运算会使用C作为进位的标志位,而V作为溢出的标志位. 一般我们学完计算机原理,都知道正数的原码反码补码都一样,而问题都 ...

  8. JS 前端 将图片转换为Base64利用H5 FileReader新特性

      file = document.getElementById("image"); var file=file.files[0]; var fileName=file.name; ...

  9. OSI参考模型概论

  10. P2421 [NOI2002]荒岛野人 扩展欧几里得 枚举

    Code: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; ...