Overloaded Constructors?!?

Discussion in 'Python' started by andrea_gavana@tin.it, Mar 20, 2005.

  1. Guest

    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
    >> are a lot of functions that, depending on the variable _type, return

    properties
    >> of the wx.Window or of the line. I would like to keep the same names

    for
    >> classes/methods, so it would be useful to have the same class with 2

    different
    >> "initializations".


    >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.
    , Mar 20, 2005
    #1
    1. Advertising

  2. Paul McGuire Guest

    >>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...


    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 .)
    Paul McGuire, Mar 21, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Rudolf
    Replies:
    12
    Views:
    8,246
    Martijn Lievaart
    Feb 6, 2004
  2. Replies:
    1
    Views:
    515
    Victor Bazarov
    Feb 7, 2005
  3. Replies:
    1
    Views:
    335
    Kent Johnson
    Mar 20, 2005
  4. iluvatar
    Replies:
    3
    Views:
    316
    Sylvester Hesp
    Feb 12, 2007
  5. Kyle Heon

    Overloaded constructors in Ruby

    Kyle Heon, Dec 6, 2005, in forum: Ruby
    Replies:
    6
    Views:
    336
    Sam Kong
    Dec 6, 2005
Loading...

Share This Page