Like overloading __init__(), but how?

Kent Johnson kent37 at tds.net
Wed Feb 23 23:44:37 EST 2005


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

This was just discussed. See
http://tinyurl.com/6zo3g

Kent

> 
> --------------------- 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.
> 



More information about the Python-list mailing list