beginner oo programming

Mike Meyer mwm at mired.org
Tue Feb 25 13:44:20 EST 2003


"Multiscan" <gcangiani at yahoo.it> writes:
> Suppose I want to define a new vector class. This is simple: a vector has
> x, y, z components.
> But suppose I want to be able to use/write/store the vectors in two (or
> more) different coordinate systems as, e.g. cartesian and oblique, and 
> that the transformation from one system to another is variable.
> For example. Suppose you have a 
> cartesian coordinate system {(1, 0), (0,1)} 
> and an oblique one {(1, 0), (a, b)} with b != 0
> The way to transform a vector from one system to the other depends on the
> ration a/b. Right ?
> Now what is the best way to add to the vectors the knowledge of the
> transformation relation ?

Steven provided a good answer to this one - you don't, you just have
objects representing the coordinate system, though he didn't touch on
the "oblique(vector)" representation.

> I think the best is always to store the vectors with a unique corrdinate
> system and then to add methods to get the coordinates on the other
> coordinate systems.

I dealt with a similar problem with modular arithemetic. I'd describe
it below.

> How do I teach to the vector object what the transformation relation 
> (the a/b ratio in the above example) is ?

You can use an instance variable, a class variable, or specify it when
you want to read/write the variable.

> Should each istance of vector class contain also a reference to the
> coordinate system object ? 

It depends on your application.

> What is the coordinate system object disappears or change address ? 

It can't in Python. Not unless you change it.

> If I have a list of vectors, how do I store only once the information on
> the reference system ?
> Should I consider inheritance ?

I used inheritance to deal with this problem, because it gave me the
behavior I wanted for my application. I had what would be a small set
of coordinates to deal with.

Create a class that knows it's coordinate system:

class Vector:
    _coordinates = Cartesian()

    def __init__(self, coord1, coord2):
        self.coord1, self.coord2 = coord1 coord2

    def set_coordinates(self, new):
        ...
    

Where set_coordinates changes the values of self.coord? appropriately.

You now have Vector objects that behave exactly like you want. So long
as you don't set the coordniates, you'll use the class variable for
it, thus not costing you any storage. If you know you're going to have
a long list of polar vectors - or some other coordinate system - then
you can subclass Vector and set the coordinate system appropriately for
that class:

class Polar_vector(Cartesian_vector):
    _coordinates = Polar()

For this class, you won't be using any extra storage so long as you
stay with a polar coordinate system.

Note that this is orthogonal to Steven's suggestion, and you can do
them both at once. In that case, can implement his "vector" with a
one-line function and some carefully constructed classes:

def vector(coord1, coord2, class):
    return class(coord1, coord2)


        <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.




More information about the Python-list mailing list