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

时间: 2024-10-17 03:45:32

Objects are mutable的相关文章

Objects constructor

Objects constructor: to have an "object type" that can be used to create many objects of one type. The standard way to create an "object type" is to use an object constructor function. <!DOCTYPE html> <html> <body> &l

Processing Images

https://developer.apple.com/library/content/documentation/GraphicsImaging/Conceptual/CoreImaging/ci_tasks/ci_tasks.html#//apple_ref/doc/uid/TP30001185-CH3-TPXREF101 Processing images means applying filters-an image filter is a piece of software that

C# - String与StringBuilder

A String object is called immutable (read-only), because its value cannot be modified after it has been created. Methods that appear to modify a String object actually return a new String object that contains the modification. Because strings are imm

Efficient Counter in Java

Reference:  http://www.programcreek.com/2013/10/efficient-counter-in-java/ You may often need a counter to understand the frequency of something (e.g., words) from a database or text file. A counter can be easily implemented by using a HashMap in Jav

Programming in Scala (Second Edition) 读书笔记18 Stateful Object

1. In previous chapters, we put the spotlight on functional (immutable) objects 在前面的章节,我们把焦点放在了函数式对象上 We did so because the idea of objects without any mutable state deserves to be better known 函数式对象更容易被理解,它永远只有一种状态 However, it is also perfectly poss

我要翻译《Think Python》-002 贡献列表 &amp; 目录部分

PDF源文件地址 :  http://www.greenteapress.com/thinkpython/thinkpython.pdf 贡献列表 自从本书诞生之后,有超过上百个目光敏锐且有想法的读者给我发来了许多建议并指出了一些需要修正的地方.他们的热情和无私的奉献给了我巨大的帮助.如果你有任何建议或者发现需要修正的地方,请发邮件至:[email protected].如果您的建议被采纳,您的大名将会出现在我们的贡献人员列表(除非你本人过于低调拒绝承认).如果您发现文中的错误内容,敬请提供一下

[Python] Understand Mutable vs. Immutable objects in Python

In this lesson, you will learn what mutable and immutable objects are, and the difference between them. This understanding will help you determine when objects can be modified in place, and when new objects must be created. List is mutable, which mea

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 show

Constructors for mutable and const versions of an iterator class

According to section 3.2 of the book "Generic Programming and STL", it is recommended to define both mutable and const versions of an iterator class. However, the constructors provided in the example code are still not perfect because some cases