Objects are mutable
2024-10-21 14:31:14
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
最新文章
- c# 生成SHA1加密字符串
- 【BZOJ-3757】苹果树 块状树 + 树上莫队
- MySQL主从分离读写复制
- OpenGL 和OpenGL ES简介
- shelll函数求两个输入数字之和
- ajax检测账户是否存在
- 解决:The Operation couldn't be completed.(LaunchServicesError error 0.)
- 利用ZABBIX的RPC-JSON作API扩展应用示例
- 201521123039 《java程序设计》第一周学习总结
- maven 控制台 打包
- .NET EF框架的安装、及三种开发模式
- UBNT ex-r +netgear gs105e v2 +ap 设置vlan 步骤记录 及相关知识整理
- Python 设计模式之路
- 在线xss练习平台
- Windows PowerShell 入門(6)-関数編1
- useful tips for python
- php : 开发记录(2017-03-10)
- 第二个spring, 第7天
- Spring+Struts+Mybatis+Shiro整合配置
- EF语句拦截器-匹配当前的Controller,Action,User