__init__ question

Gordon McMillan gmcm at hypernet.com
Fri Aug 6 16:36:17 EDT 1999

Mikael Aronsson writes:
> I have a small Python class called Vector, it is a simple 3D vector
> class with an x,y and z variable.
> I would like to have multiple constructors for this (like you can in
> C++).
> Example:
> a = Vector()  # x=0 y=0 z=0
> a = Vector( 1)  # x=1 y=1 z=1
> a = Vector( 1, 2, 3)  # x=1 y=2 z=3
> Can this be done in Python, or can I only have one __init__ method 
> ?

Just one, but 
 def __init__(self, x=0, y=0, z=0):

should get you most of the way there. Before you go wild with default 
arguments, read the FAQ on the dangers associated with using a 
mutable variable as a default argument, (not a problem here, of 

- Gordon

More information about the Python-list mailing list