Like overloading __init__(), but how?

John M. Gabriele john_sips_teaz at yahooz.com
Wed Feb 23 22:20:42 EST 2005


I know that Python doesn't do method overloading like
C++ and Java do, but how am I supposed to do something
like this:

--------------------- incorrect ------------------------
#!/usr/bin/python

class Point3d:
      pass

class Vector3d:
      """A vector in three-dimensional cartesian space."""

      def __init__( self ):
            """Create a Vector3d with some reasonable default value."""
            x, y, z = 0.0, 0.0, 0.0


      def __init__( self, x_from, y_from, z_from,
                          x_to,   y_to,   z_to ):
            """Create a Vector3d from x-y-z coords."""
            # ...
            pass


      def __init__( self, point_from, point_to ):
            """Create a Vector3d from two Point3d objects."""
            # ...
            pass
      

      def __init__( self, same_as_this_vec ):
            """Create a Vector3d from a copy of another one."""
            # ...
            pass

p = Point3d()
p2 = Point3d()
# v = Vector3d( p2, p ) -- Nope. Only the last __init__() counts.
 
---------------------- /incorrect -------------------------------

Thanks.

-- 
--- if replying via email, remove zees ---





More information about the Python-list mailing list