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: -
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?
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: -
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: - class myclass(object):
-
def __init__ (self, arg1, arg2=None):
-
if isinstance(arg1, minidom.Document):
-
<some code to parse the XML into your attrs>
-
else:
-
self.name = arg1
-
self.value = arg2
where minidom.Document is whatever type of XML document you are passing.
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: -
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.
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.
If you want the __init__'s to be seperate you could subclass but I prefer bvdet's way better.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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
|
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 ):...
|
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...
|
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 ------------------------...
|
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
|
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
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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
|
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...
|
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...
| |