Overloaded Constructors?!?

A

andrea_gavana

Hello Kent,

thank you a lot for your answer. I was starting to think that my question
was a little bit strange to obtain an answer...
This is a strange design. My first reaction is, why do you want to do that?
Maybe you >should split the class in two?

You are right. The problem is that this is not my code. Someone else has
written it and, since it is a useful widget to have in a wxPython GUI, I
was trying to port it to Python. I don't want to mess with things like SWIG,
because this widget (compiled in C++) is not portable through all platforms,
while pure Python code should be.
I could split the class in two, but I would like to keep the class/functions
definitions as closer as possible to the original one.
Next, there
One way to do this in Python is to have a single constructor that looks
at the type / >number of arguments to figure out what it is supposed to
do.

I am trying to figure it out using something like:

def __init__(self, parent, **kw):

and processing the keyword args, but it does not satisfy me very much...
Another way is to make two factory methods that
create instances of the class and do the correct initialization.

I am sorry to be so tedious, but I am still quite a newbie in Python...
could you please provide a very small example of your last sentence? Looks
quite interesting...

Thank you a lot.

Andrea.
 
P

Paul McGuire

Another way is to make two factory methods that
I am sorry to be so tedious, but I am still quite a
newbie in Python... could you please provide a very
small example of your last sentence? Looks
quite interesting...

See the recipe at this link. It defines a basic constructor to create
Color objects using 3 integer args for red/green/blue, and a separate
staticmethod as a factory method to create a Color using a single
integer representing an RBG value.

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/223611

-- Paul
(This recipe was taken from a much larger sample for basic bitmap
drawing, which you will find at
http://www.geocities.com/ptmcg/python/index.html#bmp .)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top