Python-Ref > Object oriented programming > Basics > Attribute access
 
 

<-^^->
Klíčová slova
Moduly
Knihovní funkce

Attribute access

How to prevent unwanted and unobserved change of objects data by another object.
Lets imagine an object representing a rectangle drawn on your screen. To simplify the example, the rectangle has fixed position and size but variable color represented by the attribute color.
The following simple code shows us what happens when we create such an object and then change our mind about the color and reset it from blue to red. The rectangle will remain blue until we call the draw method ourselves.
Expand/Shrink
Zdroj: (oop6-1.py)
  1   class Rectangle:
  2   
  3     def __init__( self, color):
  4       self.color = color
  5       self.draw()
  6   
  7     def draw( self):
  8       """this method draws and redraws the rectangle on the screen"""
  9       print "Redrawing to color", self.color
 10   
 11   rec = Rectangle( "blue")
 12   rec.color = "red"
 13   # here we wait for a long time and then remember to call draw
 14   print "Waiting..."
 15   rec.draw()
stdout:
Redrawing to color blue
Waiting...
Redrawing to color red
Doba běhu: 15.4 ms
Such a situation when some data bound to an object can get out of sync with its other data is very common in more complex programs. To avoid this problem, it is a common praxis and a good habit as well not to access other objects data directly but to implement and use corresponding methods. Such methods usually have names like setColor or getColor, depending of the kind of operation they do.
When such access methods are used, it is customary to provide both setXXX and getXXX methods for data that are modifiable and getXXX method for read-only data. Using only setXXX method and leaving read access to the attribute on the attribute itself is neither consistent nor user friendly.
When access to attributes is made through methods only, it is also customary to prepend the name of corresponding attribute by an underscore ("_") which is a kind of signal to the user of the class that this attribute is intended for inside usage only or by two underscores ("__") which make the attribute private (more on this feature here.
An improved example is shown below.
Expand/Shrink
Zdroj: (oop6-2.py)
  1   class Rectangle:
  2   
  3     def __init__( self, color):
  4       self.setColor( color)
  5   
  6     def setColor( self, color):
  7       """this method ensures that the change of color is also followed by redrawing of the rectangle;
  8       the color is stored in a _color attribute"""
  9       self._color = color
 10       self.draw()
 11   
 12     def getColor( self):
 13       """setXXX method is usually accompanied by a corresponding getXXX method
 14       that is used for retrieval of the corresponding attribute"""
 15       return self._color
 16   
 17     def draw( self):
 18       """this method draws and redraws the rectangle on the screen"""
 19       print "Redrawing to color", self.getColor()
 20   
 21   rec = Rectangle( "blue")
 22   rec.setColor( "red")
stdout:
Redrawing to color blue
Redrawing to color red
Doba běhu: 20.5 ms
Another way around the problem of access to attributes is by using Properties.