I am learning about classes and objects in Python. I came across the following code:
class ComplexNumber:
def __init__(self,r = 0,i = 0):
self.real = r
self.imag = i
def getData(self):
print("{0}+{1}j".format(self.real,self.imag))
# Create a new ComplexNumber object
c1 = ComplexNumber(2,3)
# Call getData() function
# Output: 2+3j
c1.getData()
I know that the first argument when ComplexNumber
is called is c1
itself which then is assigned to self
. Then in _init_
, r
is assigned to self.real
. How does it know that the type of self
is complex number. If I am not wrong, self.real
or self.imag
can only be assigned to only variables of type class 'complex'
.
But when I print the type of self it says: <class '__main__.ComplexNumber'>