473,385 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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:
Expand|Select|Wrap|Line Numbers
  1. class myclass:
  2.   __init__ (self, mystring1, mystring2)
  3.           self.name = mystring1
  4.           self.value = mystring2
  5.  
  6.   __init__ (self, xmldoc):
  7.           <some code to parse the XML into my attrs>
  8.  

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?
Jan 29 '08 #1
4 16333
bvdet
2,851 Expert Mod 2GB
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:
Expand|Select|Wrap|Line Numbers
  1. class myclass:
  2.   __init__ (self, mystring1, mystring2)
  3.           self.name = mystring1
  4.           self.value = mystring2
  5.  
  6.   __init__ (self, xmldoc):
  7.           <some code to parse the XML into my attrs>
  8.  

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:
Expand|Select|Wrap|Line Numbers
  1. class myclass(object):
  2.     def __init__ (self, arg1, arg2=None):
  3.         if isinstance(arg1, minidom.Document):
  4.             <some code to parse the XML into your attrs>
  5.         else:
  6.             self.name = arg1
  7.             self.value = arg2
where minidom.Document is whatever type of XML document you are passing.
Jan 29 '08 #2
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:

Expand|Select|Wrap|Line Numbers
  1. class myclass:
  2. def __init__(self)
  3.   c.name = ''
  4.   c.value = ''
  5.  
  6. def CreateFromStrs (mystring0, mystring1):
  7.   c = myclass
  8.   c.name = mystring0
  9.   c.value = mystring1
  10.   return c
  11. CreateFromStrings = staticmethod (CreateFromStrs)
  12.  
  13. def CreateFromXML (xmldata):
  14.   c = myclass ()
  15.   <...>
  16.   return c
  17. CreateFromXml = staticmethod (CreateFromXml)
  18.  
  19. Then later.
  20.  
  21. myclass1 = myclass.CreateFromXml (xml)
  22. 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.
Jan 29 '08 #3
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.
Jan 30 '08 #4
elcron
43
If you want the __init__'s to be seperate you could subclass but I prefer bvdet's way better.
Jan 31 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Fuzzyman | last post by:
I've been programming in python for a few months now - and returning to programming after a gap of about ten years I've really enjoyed learning python. I've just made my first forays into...
2
by: Sergey Krushinsky | last post by:
Hello all, Is there a common way to emulate constructor overloading in Python class? For instanse, I have 3 classes: 1/ Polar - to hold polar coordinates; 2/ Cartesian - to hold cartesian...
33
by: Jacek Generowicz | last post by:
I would like to write a metaclass which would allow me to overload names in the definition of its instances, like this class Foo(object): __metaclass__ = OverloadingClass att = 1 att = 3
7
by: Doran_Dermot | last post by:
Hi All, I've seen lots of code in which the attributes of a class are accessed and modified using two separate methods. For example: class Problems: def __init__( self, refNum ):...
9
by: Martin Häcker | last post by:
Hi there, I just tried to run this code and failed miserably - though I dunno why. Could any of you please enlighten me why this doesn't work? Thanks a bunch. --- snip --- import unittest...
4
by: John M. Gabriele | last post by:
I know that Python doesn't do method overloading like C++ and Java do, but how am I supposed to do something like this: --------------------- incorrect ------------------------...
3
by: Iyer, Prasad C | last post by:
I am new to python. I have few questions a. Is there something like function overloading in python? b. Can I overload __init__ method Thanks in advance regards
11
by: placid | last post by:
Hi all, Is it possible to be able to do the following in Python? class Test: def __init__(self): pass def puts(self, str): print str
9
by: sturlamolden | last post by:
Python allows the binding behaviour to be defined for descriptors, using the __set__ and __get__ methods. I think it would be a major advantage if this could be generalized to any object, by...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.