PI. Classes into. Complex number

Run Settings
LanguagePython
Language Version
Run Command
# Import our class form outer module from complex import Complex # Create a new Complex class instance comp = Complex(4, 3) print comp print comp.abs
# Class definition for complex number. More details on Complex number is # here: https://en.wikipedia.org/wiki/Complex_number class Complex: """A complex number is a number that can be expressed in the form a + bi, where a and b are real numbers and i is the imaginary unit, that satisfies the equation i**2=-1.""" # The line above called "the doc string" and would appears in all hints and # __doc__ attribute later. # This is the instance constructor, place to initialize values of exact # instance, please do not confuse it with __new__ which is more complicated # topic of for next level lecture. def __init__(self, re, im=0): self.re = re self.im = im def __repr__(self): # Method to create object representation. return "{re} + {im}i".format(**self.__dict__) # Following line means that name of this method would be used as name of # read-only property (to make writable one there additional syntax) @property def abs(self): return (self.re**2 + self.im**2)**0.5
Editor Settings
Theme
Key bindings
Full width
Lines