码迷,mamicode.com
首页 > 其他好文 > 详细

Objects are mutable

时间:2014-09-30 02:18:01      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   for   sp   

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

标签:style   blog   http   color   io   os   ar   for   sp   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!