Attributes

You can assign values to an instance using dot notation. This syntax for selecting a variable from a module, such as math.pi or string.uppercase. In this case, though, we are assigning values to named elements of an object. These elements are called attributes. The following diagram shows the result of these assignments. A state diagram that shows an object and its attributes is called an object diagram:

                         

The variable blank refers to a Point object, which contains two attributes. Each attribute refers to a floating-point number. The expression blank.x means, ‘Go to the object blank refers to and get the value of x’. In this case, we assign that value to a variable named x. There is no conflict between the variable x and the attribute x. You can use dot notation as part of any expression. And you can pass an instance as an argument in the usual way. For example:

 

Inside the function, p is an alias for blank, so if the function modifies p, blank changes.

 

from Thinking in Python

posted @ 2014-09-25 14:58  平静缓和用胸音说爱  阅读(266)  评论(0编辑  收藏  举报