When to use classes?

Daniel T. danielt3 at gte.net
Sun Mar 5 12:12:08 EST 2000


Scott Bahling <sbahling at pitnet.net> wrote:

>First: When and when not to create a new class?

Use the same criteria you would use in any other language. One place to
look for ideas that is more applicable to Python than most OO Books is
"Refactoring" by Martin Fowler. Yes the examples are in java but examples
are simple enough that you won't get tripped up by the syntax.

> An example would be if I
>have a class for a Polygon, I need to store a set of points defining the
>polygon object. One method would be to store a list of tuples containing
>the polygon's points. If I want to access the individual x and y coords
>of each point, I end up with code that looks like:
>                
>                firstxcoord = poly.points[0][0]
>                firstycoord = poly.points[0][1]
>
>Or I could create a Point class and use a list of them in the Polygon
>class. Then I would have code that looks like:
>        
>                firstxcoord = poly.points[0].x
>                firstycoord = poly.points[0].y

For the above, your best bet would be something like:

x,y = poly.firstPointX(), poly.firstPointY()

or

x,y = poly.firstPoint() # assumes that firstPoint( self ) returns a tuple

or

aPoint = poly.firstPoint()  # assumes that firstPoint( self ) returns a Point
                            # class object

You will notice that now, the client of poly has no idea how any of the
x,y points are contained, nor does it care. This is a good thing, this way
you can change the Polygon class to suit your own tastes without worrying
about it. (Read up on the "Law of Demeter")

Actually there should be no need for the left side of the assignment. Just
use poly.firstPoint() directly.

>The second piece of code is easier to read. But lets say that this Point
>class has no other purpose but to contain two numerical values; is the
>use of a Point class overkill? Is there a 'rule of thumb' for when to
>create a new class?

Again, the Refactoring book will give you some ideas of when to create a
new class. The best thing about the book vis-a-vis Python is that you just
write the code however you can to get it working, then you can use the
techniques in the book to massage it into shape. This goes very well with
Pythons interpretive quick turnaround nature.

>Second: What is the general consensus about accessing instance variables
>directly like myobj.x=10 vs. through methods like myobj.setx(10). The
>first method is quicker/easier to code, but is it "bad" coding?  Does
>anyone even care?

Just because Python doesn't have any private parts (except for the
self.__x idiom) doesn't mean that encapsulation is suddenly a bad thing.
There is no harm in accessing an objects state, but don't change that
objects state from outside the object. This would include calling a
mutator on your points object from outside your poly object above.

-- 
When a thing ceases to be a subject of controversy,
it ceases to be a subject of interest.
                                                   -- William Hazlitt



More information about the Python-list mailing list