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. POJ 1107

    水题一道,注意取模时不能为0 #include <iostream> #include <algorithm> #include <cstring> #includ ...

  2. 字符串中连续出现最多的子串 &amp; 字符串中最长反复子串

    字符串中连续出现最多的子串 & 字符串中最长反复子串 字符串中连续出现最多的子串 & 字符串中最长反复子串,这两个问题都能够用后缀数组来表示,至于后缀数组能够參考编程珠玑P156:后缀 ...

  3. Go语言核心之美 1.1-命名篇

    命名篇 1.Go的函数.变量.常量.自己定义类型.包(Package)的命名方式遵循以下规则: 1)首字符能够是随意的Unicode字符或者下划线 2)剩余字符能够是Unicode字符.下划线.数字 ...

  4. Thrift源代码分析(七)-- TServerserver分析

    Thrift採用了TServer来作为server的抽象,提供了多种类型的server实现.用TServerTransport作为server的Acceptor抽象,来监听端口.创建clientSoc ...

  5. poj1363——Rails

    Description There is a famous railway station in PopPush City. Country there is incredibly hilly. Th ...

  6. swift -函数、函数指针

    // // main.swift // FunctionTest-04 // import Foundation println("函数測试!") testFunction() / ...

  7. MongoDB 系列(一) C# 类似EF语法简单封装

    之前写过一篇关于MongoDB的封装 发现太过繁琐 于是打算从新写一篇简易版 1:关于MongoDB的安装请自行百度,进行权限认证的时候有一个小坑,3.0之后授权认证方式默认的SCRAM-SHA-1模 ...

  8. CSS中元素各种居中方法(思维导图)

    前言 用思维导图的方式简单总结一下各种元素的居中方法,如下图: 补充一下: table自带功能 100% 高度的 afrer before 加上 inline block优化 div 装成 table ...

  9. 乌班图 之 apt命令 及 VMware共享文件夹

    apt是Advanced Packaging Tool ,是Ubuntu下的一个安装包管理工具 大部分软件的安装.更新.卸载 都是利用apt命令来实现 直接在终端输入apt即可查阅命令的帮助信息 常用 ...

  10. 阿里云ecs : Couldn't connect to host, port: smtp.aliyun.com, 25; timeout -1;

    上传到服务器后javamail发邮件异常 链接 原来是ECS基于安全考虑,禁用了端口25. 改成465就可以发邮件了. p.setProperty("mail.smtp.socketFact ...