Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Attribute Access Exercises

  1. Rework Previous Exercises. Refer to exercises for previous chapters (the section called “Class Definition Exercises”, the section called “Advanced Class Definition Exercises”, the section called “Design Pattern Exercises”, the section called “Special Method Name Exercises”). Rework these exercises to manage attributes with getters and setters. Use the property function to bind a pair of getter and setter functions to an attribute name. The following examples show the "before" and "after" of this kind of transformation.

    class SomeClass( object ):
        def __init__( self, someValue ):
            self.myValue= someValue
    

    When we introduce the getter and setter method functions, we should also rename the original attribute to make it private. When we define the property, we can use the original attribute's name. In effect, this set of transformations leaves the class interface unchanged. We have added the ability to do additional processing around attribute get and set operations.

    class SomeClass( object ):
        def __init__( self, someValue ):
            self._myValue= someValue
        def getMyValue( self ):
            return self._myValue
        def setMyvalue( self, someValue ):
            self._myValue= someValue
        myValue= property( getMyValue, setMyValue )

    The class interface should not change when you replace an attibute with a property. The original unit tests should still work perfectly.

  2. Rework Previous Exercises. Refer to exercises for previous chapters (the section called “Class Definition Exercises”, the section called “Advanced Class Definition Exercises”, the section called “Design Pattern Exercises”, the section called “Special Method Name Exercises”). Rework these exercises to manage attributes with Descriptors. Define a Desciptor class with __get__ and __set__ methods for an attribute. Replace the attribute with an instance of the Descriptor.

    When we introduce a descriptor, our class should look something like the following.

    class ValueDescr( object ):
        def __set__( self, instance, value ):
            instance.value= value
        def __get__( self, instance, owner ):
            return instance.value
    
    class SomeClass( object ):
        def __init__( self, someValue ):
            self.myValue= ValueDescr()
    

    The class interface should not change when you replace an attibute with a descriptor. The original unit tests should still work perfectly.

  3. Tradeoffs and Design Decisions. What is the advantage of Python's preference for referring to attributes directly instead of through getter and setter method functions?

    What is the advantage of having an attribute bound to a property or descriptor instead of an instance variable?

    What are the potential problems with the indirection created by properties or descriptors?


 
 
  Published under the terms of the Open Publication License Design by Interspire