how to use more than 1 __init__ constructor in a class ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • scott

    how to use more than 1 __init__ constructor in a class ?

    hi people,

    can someone tell me, how to use a class like that* (or "simulate" more
    than 1 constructor) :
    #--
    class myPointClass:
    def __init__(self, x=0, y=0):
    self.x = x
    self.y = y
    def __init__(self, x=0, y=0, z=0):
    self.__init__(s elf, x, y)
    self.z = z
    #--

    tia people
    scott

    *this is not homework
  • Michael Hoffman

    #2
    Re: how to use more than 1 __init__ constructor in a class ?

    scott wrote:
    [color=blue]
    > can someone tell me, how to use a class like that* (or "simulate" more
    > than 1 constructor) :
    > #--
    > class myPointClass:
    > def __init__(self, x=0, y=0):
    > self.x = x
    > self.y = y
    > def __init__(self, x=0, y=0, z=0):
    > self.__init__(s elf, x, y)
    > self.z = z
    > #--[/color]

    Well for the example you used, multiple constructors are not needed.
    This will get you the same result as what I imagine you wanted the
    first example to do:

    class myPointClass:
    def __init__(self, x=0, y=0, z=0):
    self.x = x
    self.y = y
    self.z = z
    --
    Michael Hoffman

    Comment

    • Steve

      #3
      Re: how to use more than 1 __init__ constructor in a class ?

      Hi Scott,[color=blue]
      > can someone tell me, how to use a class like that* (or "simulate" more
      > than 1 constructor) :[/color]

      One approach could be:

      class myPointClass:
      def __init__(self, **args):
      for k, v in args.items():
      setattr(self, k, v)
      [color=blue]
      > *this is not homework[/color]
      Just to be safe, I'll leave out the explanation :)

      Regards
      Steve

      On 6/22/05, scott <scott@alussina n.org> wrote:[color=blue]
      > hi people,
      >
      > can someone tell me, how to use a class like that* (or "simulate" more
      > than 1 constructor) :
      > #--
      > class myPointClass:
      > def __init__(self, x=0, y=0):
      > self.x = x
      > self.y = y
      > def __init__(self, x=0, y=0, z=0):
      > self.__init__(s elf, x, y)
      > self.z = z
      > #--
      >
      > tia people
      > scott
      >
      > *this is not homework
      > --
      > http://mail.python.org/mailman/listinfo/python-list
      >[/color]

      Comment

      • Paul McGuire

        #4
        Re: how to use more than 1 __init__ constructor in a class ?

        This recipe that I submitted to the Python Cookbook
        (http://aspn.activestate.com/ASPN/Coo.../Recipe/223611)
        describes a technique for doing this. I use the example of creating
        Color objects for plotting to a bitmap, using either R,G,andB values,
        or a single integer representing the RGB encoded value.

        This style you describe is a Java language artifact. If you have
        optional params, then these really don't represent different method
        signatures. In my color example, you truly have different signatures,
        either 3 integers representing color components, or a single encoded
        integer.

        -- Paul

        Comment

        • Rocco Moretti

          #5
          Re: how to use more than 1 __init__ constructor in a class ?

          scott wrote:[color=blue]
          > hi people,
          >
          > can someone tell me, how to use a class like that* (or "simulate" more
          > than 1 constructor) :
          > #--
          > class myPointClass:
          > def __init__(self, x=0, y=0):
          > self.x = x
          > self.y = y
          > def __init__(self, x=0, y=0, z=0):
          > self.__init__(s elf, x, y)
          > self.z = z
          > #--
          >[/color]

          You might try:

          #--
          class myPointClass:
          def __init__(self, x=0, y=0, z=None):
          self.x = x
          self.y = y
          if z is not None:
          self.z = z
          #--

          You could also turn __init__ into a dispatch fuction:

          #--
          class myPointClass:
          def __init__(self, *args):
          if len(args) <= 2:
          self.__init_two (*args)
          if len(args) == 3:
          self.__init_thr ee(*args)
          def __init_two(self , x=0, y=0):
          self.x = x
          self.y = y
          def __init_three(se lf, x=0, y=0, z=0):
          self.__init_two (x, y)
          self.z = z
          #--

          But I would definitely recommend looking at your algorithm to determine
          if there is a better way to do what you want, that doesn't require an
          initilizer with two different signatures.

          Comment

          • wittempj@hotmail.com

            #6
            Re: how to use more than 1 __init__ constructor in a class ?

            You also could use a list to represent your data, then you get more
            dimensions supported, e.g:
            import math
            class Point:
            def __init__(self, *args):
            self.points = list(args)

            def dist(x, y):
            if len(x.points) != len(y.points):
            raise RuntimeError('d imensions not the same')
            d = 0
            for i in range(len(x.poi nts)):
            d += (x.points[i] - y.points[i])**2
            return math.sqrt(d)

            Comment

            • F. Petitjean

              #7
              Re: how to use more than 1 __init__ constructor in a class ?

              Le 22 Jun 2005 11:44:09 -0700, wittempj@hotmai l.com a écrit :[color=blue]
              > You also could use a list to represent your data, then you get more
              > dimensions supported, e.g:
              > import math
              > class Point:
              > def __init__(self, *args):
              > self.points = list(args)
              >
              > def dist(x, y):
              > if len(x.points) != len(y.points):
              > raise RuntimeError('d imensions not the same')
              > d = 0
              > for i in range(len(x.poi nts)):
              > d += (x.points[i] - y.points[i])**2
              > return math.sqrt(d)
              >[/color]

              My rewrite (same idea) :-)
              class Point(object):
              def __init__(self, *args):
              self.coords = list(args) # or even args ?

              def dist(self, other):
              d2 = sum([ (c1-c2)*(c1-c2) for c1, c2 in zip(self.coords ,
              other.coords)])
              return math.sqrt(d2)


              Comment

              • Roy Smith

                #8
                Re: how to use more than 1 __init__ constructor in a class ?

                scott <scott@alussina n.org> wrote:[color=blue]
                >hi people,
                >
                >can someone tell me, how to use a class like that* (or "simulate" more
                >than 1 constructor) :
                >#--
                >class myPointClass:
                > def __init__(self, x=0, y=0):
                > self.x = x
                > self.y = y
                > def __init__(self, x=0, y=0, z=0):
                > self.__init__(s elf, x, y)
                > self.z = z[/color]

                Python does not have the kind of polymorphism that C++ or Java has.
                There is only a single copy of each method (including __init__) for
                each class, but methods can take variable numbers of arguments, with
                default values. You probably want something like:

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

                You can call this with 0, 1, or 2 arguments, i.e. any of the following
                are legal:

                MyClass() # x, y, and z all get defaulted to 0
                MyClass(1) # x=1, y and z both default to 0
                MyClass(1, 2) # x=1, y=2, z defaults to 0
                MyClass(1, 2, 3) # x=1, y=2, z=3

                Once you get the hang of it, you'll understand just how brain-dead C++
                and Java really are :-)

                Take a look at http://docs.python.org/ref/function.html for more
                details.


                Comment

                • Jeffrey Maitland

                  #9
                  Re: how to use more than 1 __init__ constructor in a class ?

                  Well one way to do this (not sure if it is the best way) is something like.

                  class mypoint:
                  def __init__(self, *args):
                  len_args = len(args)
                  print len_args
                  if len_args == 0:
                  self.x = 0
                  self.y = 0
                  self.z = 0
                  elif len_args >=2 and len_args <= 3:
                  for i in range(len_args) :
                  if args[i] == None: args[i] = 0 #populating the list of
                  args with defgault values if null(None)
                  self.x = kargs[0]
                  self.y = kargs[1]
                  if len_args == 3: self.z = args[2]
                  else: raise "Invalid Input"

                  Now this is not looking for null input such as
                  p = mypointclass(,, ,)
                  this is an invalid syntax error. Now there must be away to catch for
                  the blanks but Iam not sure off the top of my head.
                  hope that helps


                  On 6/22/05, scott <scott@alussina n.org> wrote:[color=blue]
                  > hi people,
                  >
                  > can someone tell me, how to use a class like that* (or "simulate" more
                  > than 1 constructor) :
                  > #--
                  > class myPointClass:
                  > def __init__(self, x=0, y=0):
                  > self.x = x
                  > self.y = y
                  > def __init__(self, x=0, y=0, z=0):
                  > self.__init__(s elf, x, y)
                  > self.z = z
                  > #--
                  >
                  > tia people
                  > scott
                  >
                  > *this is not homework
                  > --
                  > http://mail.python.org/mailman/listinfo/python-list
                  >[/color]

                  Comment

                  • Steven D'Aprano

                    #10
                    Re: how to use more than 1 __init__ constructor in a class ?

                    On Wed, 22 Jun 2005 12:34:21 -0500, Rocco Moretti wrote:
                    [color=blue]
                    > scott wrote:[color=green]
                    >> hi people,
                    >>
                    >> can someone tell me, how to use a class like that* (or "simulate" more
                    >> than 1 constructor) :
                    >> #--[/color][/color]

                    [snip]
                    [color=blue]
                    > You could also turn __init__ into a dispatch fuction:
                    >
                    > #--
                    > class myPointClass:
                    > def __init__(self, *args):
                    > if len(args) <= 2:
                    > self.__init_two (*args)
                    > if len(args) == 3:
                    > self.__init_thr ee(*args)[/color]

                    Oh wow, so that's what I've been doing for years. Dispatching.

                    And I thought I was just calling other functions :-)

                    That's the joys of a mostly self-taught programming knowledge: you miss
                    out on all the buzzwords.


                    --
                    Steven.

                    Comment

                    • Rocco Moretti

                      #11
                      Re: how to use more than 1 __init__ constructor in a class ?

                      Steven D'Aprano wrote:[color=blue]
                      > On Wed, 22 Jun 2005 12:34:21 -0500, Rocco Moretti wrote:
                      >
                      >[color=green]
                      >>You could also turn __init__ into a dispatch fuction:
                      >>
                      >>#--
                      >>class myPointClass:
                      >> def __init__(self, *args):
                      >> if len(args) <= 2:
                      >> self.__init_two (*args)
                      >> if len(args) == 3:
                      >> self.__init_thr ee(*args)[/color]
                      >
                      >
                      > Oh wow, so that's what I've been doing for years. Dispatching.
                      >
                      > And I thought I was just calling other functions :-)[/color]

                      I think the distinction between just calling other functions and
                      dispatching is that with dispatching, the function doesn't do any actual
                      work by itself, but just hands off the work to a different function.


                      [color=blue]
                      > That's the joys of a mostly self-taught programming knowledge: you miss
                      > out on all the buzzwords.[/color]

                      Being mostly self taught myself, I have a tendancy to use infrequently
                      encountered terms in related but technically inappropriate contexts,
                      confusing the better informed people I deal with. ;-)

                      Comment

                      • Singletoned

                        #12
                        Re: how to use more than 1 __init__ constructor in a class ?

                        Rocco Moretti wrote:[color=blue]
                        > Steven D'Aprano wrote:[/color]
                        <snip>[color=blue][color=green]
                        > > That's the joys of a mostly self-taught programming knowledge: you miss
                        > > out on all the buzzwords.[/color]
                        >
                        > Being mostly self taught myself, I have a tendancy to use infrequently
                        > encountered terms in related but technically inappropriate contexts,
                        > confusing the better informed people I deal with. ;-)[/color]

                        Indeed. I find I use even more buzzwords because I can just make up as
                        many as I want.

                        Comment

                        • jean-marc

                          #13
                          Re: how to use more than 1 __init__ constructor in a class ?



                          Singletoned wrote:[color=blue]
                          > Rocco Moretti wrote:[color=green]
                          > > Steven D'Aprano wrote:[/color]
                          > <snip>[color=green][color=darkred]
                          > > > That's the joys of a mostly self-taught programming knowledge: you miss
                          > > > out on all the buzzwords.[/color]
                          > >
                          > > Being mostly self taught myself, I have a tendancy to use infrequently
                          > > encountered terms in related but technically inappropriate contexts,
                          > > confusing the better informed people I deal with. ;-)[/color]
                          >
                          > Indeed. I find I use even more buzzwords because I can just make up as
                          > many as I want.[/color]
                          This thread 'branch' (humm, is this an appropriate term for the last
                          few quotes, going to Steven's?) is soothing in reminding us we are not
                          alone. That there is a sort of distributed 'Alma Mater' of the
                          'Teach-It-Yourself School of Computing', producing a virtual FOAF group
                          (Is FOAF, Friend Of A Friend or Flock Of A Feather?)

                          jm

                          Comment

                          • Terry Hancock

                            #14
                            Re: how to use more than 1 __init__ constructor in a class ?

                            On Thursday 23 June 2005 10:31 am, Singletoned wrote:[color=blue]
                            > Rocco Moretti wrote:[color=green]
                            > > Steven D'Aprano wrote:[/color]
                            > <snip>[color=green][color=darkred]
                            > > > That's the joys of a mostly self-taught programming knowledge: you miss
                            > > > out on all the buzzwords.[/color]
                            > >
                            > > Being mostly self taught myself, I have a tendancy to use infrequently
                            > > encountered terms in related but technically inappropriate contexts,
                            > > confusing the better informed people I deal with. ;-)[/color]
                            >
                            > Indeed. I find I use even more buzzwords because I can just make up as
                            > many as I want.[/color]

                            As fun as that is ;-), it does make it hard to communicate. It seems to me
                            that I spend a lot of time trying to figure out what buzzwords mean, only
                            to find that 9/10ths of them are for things I've already done somewhere.

                            Of course, that 10th one can be a doozy, "metaclasse s" comes to mind.

                            And it does make it easier to find help on problems, if you know what
                            other people call them.

                            --
                            Terry Hancock ( hancock at anansispacework s.com )
                            Anansi Spaceworks http://www.anansispaceworks.com

                            Comment

                            Working...