FAQ
Hi!

In C++ you can overload functions and constructors. For example if I have a
class that represents a complex number, than it would be nice if I can
write two seperate constructors

class Complex:

def __init__(self):
self.real=0
self.imag=0

def __init__self(self,r,i):
self.real=r
self.imag=i


How would I do this in python?

And by the way, is it possible to overload operators like +,-,*?

def operator+(self,complex2):
Complex res
res.real=self.real+complex2.real
res.imag=self.imag+complex2.imag

return res


thanks!

Search Discussions

  • Jean-Paul Calderone at Dec 9, 2005 at 5:45 pm

    On Fri, 09 Dec 2005 18:29:12 +0100, Johannes Reichel wrote:
    Hi!

    In C++ you can overload functions and constructors. For example if I have a
    class that represents a complex number, than it would be nice if I can
    write two seperate constructors

    class Complex:

    def __init__(self):
    self.real=0
    self.imag=0

    def __init__self(self,r,i):
    self.real=r
    self.imag=i
    class Complex:
    def __init__(self, r=0, i=0):
    self.real = r
    self.imag = i
    How would I do this in python?

    And by the way, is it possible to overload operators like +,-,*?

    def operator+(self,complex2):
    Complex res
    res.real=self.real+complex2.real
    res.imag=self.imag+complex2.imag

    return res
    def __add__(self, complex2):
    res = Complex(self.real + complex2.real, self.imag + complex2.imag)
    return res

    Jean-Paul
  • Jepler at Dec 9, 2005 at 5:47 pm

    On Fri, Dec 09, 2005 at 06:29:12PM +0100, Johannes Reichel wrote:
    Hi!

    In C++ you can overload functions and constructors. For example if I have a
    class that represents a complex number, than it would be nice if I can
    write two seperate constructors
    Python doesn't support this, but it does support default arguments:
    class Complex:
    def __init__(self, real=0, imag=0):
    self.real = real
    self.imag = imag
    And by the way, is it possible to overload operators like +,-,*?

    def operator+(self,complex2):
    The special methods have names like __add__.
    http://docs.python.org/ref/numeric-types.html

    Jeff
    -------------- next part --------------
    A non-text attachment was scrubbed...
    Name: not available
    Type: application/pgp-signature
    Size: 189 bytes
    Desc: not available
    Url : http://mail.python.org/pipermail/python-list/attachments/20051209/6d92c240/attachment.pgp
  • Christopher Subich at Dec 9, 2005 at 6:49 pm

    Johannes Reichel wrote:
    Hi!

    In C++ you can overload functions and constructors. For example if I have a
    class that represents a complex number, than it would be nice if I can
    write two seperate constructors

    class Complex:
    Please do note, if you want this for the exact use of a Complex class,
    Python does have complex arithmetic built-in:

    print (0 + 1j)

    The other posted points are still valid (and generally applicable).
  • Josef Meile at Dec 9, 2005 at 7:26 pm

    In C++ you can overload functions and constructors. For example if I have a
    class that represents a complex number, than it would be nice if I can
    write two seperate constructors

    Python doesn't support this, but it does support default arguments:
    Yes, in part you are right since the python core doesn't include them.
    However they can be implemented with decorators:

    * Subject: decorators and multimethods
    Group: comp.lang.python
    Link: http://tinyurl.com/d45ym

    Anyway, as you, I also use the default arguments.

    Regards
    Josef
  • Josef Meile at Dec 9, 2005 at 7:49 pm

    In C++ you can overload functions and constructors. For example if I
    have a
    class that represents a complex number, than it would be nice if I can
    write two seperate constructors


    Python doesn't support this, but it does support default arguments:
    Yes, in part you are right since the python core doesn't include them.
    However they can be implemented with decorators:

    * Subject: decorators and multimethods
    Group: comp.lang.python
    Link: http://tinyurl.com/d45ym
    There is even a better recipe, explained step by step from Guido:
    * Five-minute Multimethods in Python
    Link: http://tinyurl.com/8ppd6

    Regards
    Josef

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedDec 9, '05 at 5:29p
activeDec 9, '05 at 7:49p
posts6
users5
websitepython.org

People

Translate

site design / logo © 2023 Grokbase