Overloading __init__

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Benny the Guard
    New Member
    • Jun 2007
    • 92

    Overloading __init__

    Working on a class that I would use multiple constructors in C++ since I have different ways of creating the data. Tried this in python by defining multiple __init__ methods but to no avail, it seems to only find the second one. So I have:
    Code:
    class myclass:
      __init__ (self, mystring1, mystring2)
              self.name = mystring1
              self.value = mystring2
    
      __init__ (self, xmldoc):
              <some code to parse the XML into my attrs>

    Now my class is way more complex then that, but I just want multiple __init__ methods, with different signatures, to insantite my objects. When i try to use the first one it says "TypeError: __init__() takes exactly 2 arguments (3 given)" using the second version of __init__ does work. Thoughts?
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by Benny the Guard
    Working on a class that I would use multiple constructors in C++ since I have different ways of creating the data. Tried this in python by defining multiple __init__ methods but to no avail, it seems to only find the second one. So I have:
    Code:
    class myclass:
      __init__ (self, mystring1, mystring2)
              self.name = mystring1
              self.value = mystring2
    
      __init__ (self, xmldoc):
              <some code to parse the XML into my attrs>

    Now my class is way more complex then that, but I just want multiple __init__ methods, with different signatures, to insantite my objects. When i try to use the first one it says "TypeError: __init__() takes exactly 2 arguments (3 given)" using the second version of __init__ does work. Thoughts?
    It makes sense that only the second __init__ would work. Why not something like this:[code=Python]class myclass(object) :
    def __init__ (self, arg1, arg2=None):
    if isinstance(arg1 , minidom.Documen t):
    <some code to parse the XML into your attrs>
    else:
    self.name = arg1
    self.value = arg2[/code]where minidom.Documen t is whatever type of XML document you are passing.

    Comment

    • Benny the Guard
      New Member
      • Jun 2007
      • 92

      #3
      That works, just seems counter-intuitive since all other methods can be overloaded (and it's a basic element of OO design). If overriding __init__ is not possible, can Python really claim to be fully OO?

      What I just tried was using staticmethod factories to create it, this keeps my logic separate (and my real init logic is pretty big for each case). So I have:

      Code:
      class myclass:
      def __init__(self)
        c.name = ''
        c.value = ''
      
      def CreateFromStrs (mystring0, mystring1):
        c = myclass
        c.name = mystring0
        c.value = mystring1
        return c
      CreateFromStrings = staticmethod (CreateFromStrs)
      
      def CreateFromXML (xmldata):
        c = myclass ()
        <...>
        return c
      CreateFromXml = staticmethod (CreateFromXml)
      
      Then later.
      
      myclass1 = myclass.CreateFromXml (xml)
      myclass2 = myclass.CreateFromStrs ('hellow', 'world')

      I will chalk it up to one of those Python oddities. Minor annoyance, just makes me comment more, which is probably good anyway. Thanks for the help.

      Comment

      • William Manley
        New Member
        • Mar 2007
        • 56

        #4
        but python *does* override it. It defines the first __init__ function and then, overrides it, when you define the second __init__ function.

        its not a matter of being OO, its a matter of using the same name.

        Comment

        • elcron
          New Member
          • Sep 2007
          • 43

          #5
          If you want the __init__'s to be seperate you could subclass but I prefer bvdet's way better.

          Comment

          Working...