Python: Classes

Suppose you have a below class in python:

class Complex:
def __init__(self, realpart, imagpart):
self.r = realpart
self.i = imagpart

>>> x = Complex(3.0, -4.5)

What happens? Even though you have not given the arguments for the class, the arguments get initialized to the default constructor.Therefore 3.0 and -4.5 will be set to realpart and imagpart variables.
>>> x.r, x.i
(3.0, -4.5)

But if you try this in Java, you are surely going to get an error without a constructor for this with arguments.


Leave a Reply

Your email address will not be published. Required fields are marked *