首页 > 其他 > 详细

Objects are mutable

时间:2014-09-30 00:35:52      阅读:284      评论:0      收藏:0      [点我收藏+]

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:

 bubuko.com,布布扣                      

Inside the function, rect is an alias for box, so if the function modifies rect, box changes.

 

from Thinking in Python

Objects are mutable

原文:http://www.cnblogs.com/ryansunyu/p/4001147.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!