FAQ
Hi !

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 ?

Mikael

Search Discussions

  • Gordon McMillan at Aug 6, 1999 at 8:36 pm

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




    - Gordon
  • Adrian Eyre at Aug 10, 1999 at 12:52 pm

    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
    def __init__(self, x=0, y=0, z=0):
    How about:
    class Vector:
    ... def __init__(self, *args):
    ... print args
    ...
    v=Vector()
    ()
    v=Vector(1)
    (1,)
    v=Vector(1,2)
    (1, 2)

    ...etc.

    You can then parse the args in the constructor.

    --------------------------------------------
    Adrian Eyre <mailto:a.eyre at optichrome.com>
    Optichrome Computer Solutions Ltd
    Maybury Road, Woking, Surrey, GU21 5HX, UK
    Tel: +44 1483 740 233 Fax: +44 1483 760 644
    http://www.optichrome.com
    --------------------------------------------
  • Moshe Zadka at Aug 10, 1999 at 1:24 pm
    [Adrian Eyre, in response to the following challenge:

    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]

    How about:
    class Vector:
    ... def __init__(self, *args):
    ... print args
    ...
    v=Vector()
    ()
    v=Vector(1)
    (1,)
    v=Vector(1,2)
    (1, 2)


    The only problem with this solution is that you have to parse
    args yourself (raising TypeErrors for wrong number of args, etc.),
    and having no keyword arguments, as in

    Vector(z=5) # x=0, y=0

    A quicker, easier and more intuitive solution is:

    class Vector:

    def __init__(self, x=0, y=0, z=0):
    self.x, self.y, self.z = x, y, z


    all examples from the original post, plus my own, work with the added
    benefit Python does all argument parsing for us.
  • Adrian Eyre at Aug 11, 1999 at 8:52 am

    class Vector:

    def __init__(self, x=0, y=0, z=0):
    self.x, self.y, self.z = x, y, z


    all examples from the original post, plus my own, work with the added
    benefit Python does all argument parsing for us.
    True, but I got the impression from the original post that the number
    of allowed arguments may be unlimited.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedAug 6, '99 at 7:28p
activeAug 11, '99 at 8:52a
posts5
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase