When to use classes?
Ken Starks
ken at straton.demon.co.uk
Sun Mar 5 10:59:37 EST 2000
Scott Bahling <sbahling at pitnet.net> wrote:
... [ General description of a polygon class ]
> firstxcoord = poly.points[0].x firstycoord = poly.points[0].y
> 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?
> 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?
When I started Python I also wrote a few 'vector graphics' classes.
I used a class for points and for polygons, and I both set and read
the coordinates using methods.
This was useful for two main reasons:
1. I wanted the user to use mm or inches or some other proper unit
It could be floating point.
I could make it 'snap' to the nearest grid point.
Internally, the coordinates were fixed point integers related
to pixel size, postscript, and printer's points; the user
did not have to know that!
2. The polygon class had a few derived classes. In the original
class the shape was held as a sequence of points, but in the
derived class of 'regular' polygons it was calculated.
The method xxx.getpoints() returned a sequence in both cases,
but in the second case it was 'virtual' if you like.
The same sort of thing applied to some 'curve stitching' shapes
in which two sequences of points are joined to create an
'envelope'. This was for some evolute/involute geometry
that I never finished.
3. Actually, I tell a lie. The original class was for a path
which could contain gaps, could be open or closed, and which
could include Bezier segments. It had its own local origin and
scale factor.
The 'Polygon' was a derived class of this which was always closed
and which had straight segments between all the points.
But the main point is that the class-based design will allow me
use all the line-thickness, fill-colour, Etc methods on any
new objects I come up with.
Ken, __O
_-\<,_
(_)/ (_)
Virtuale Saluton.
More information about the Python-list
mailing list