Python Questions. Send Code via .py files please

Python Dr. Omar EL Khatib Classes A class creates a new local namespace where all its attributes are defined. Attributes may be data (variables) or functions . class Person(): "This is a person class" # used for __doc__ functions def greet(self): self.name = "James" # class variables print ('Hello ', self.name) harry = Person() # Output: print( harry.greet ) # Output: Hello James print( harry.greet ()) # Output: 'This is a person class ' print( harry.__ doc __) 2 Classes class ComplexNumber : def __ init __(self, r=0, i=0): self .real = r # local variables self .imag = i def get_data (self): print (self.real , ' + ', self.imag + 'j') # Create anew ComplexNumber instance num1 = ComplexNumber (2, 3) # calling method get_data () # output: 2+3j num1.get_data() # create another ComplexNumber instance: num2 = ComplexNumber (5) # both output: 5+j0 num2.get_data() print (num2.real, "+", num2.imag, "j") 3 Class Inheritance • Inheritance is a powerful feature in object oriented programming. • It refers to defining a new class with little or no modification to an existing class. The new class is called derived (or child) class and the one from which it inherits is called the base (or parent) class . class BaseClass : Body of base class class DerivedClass (BaseClass ): Body of derived class 4 Base class Child class Class Inheritance class Polygon: def __ init __(self, no_of_sides ): self.n = no_of_sides self.sides = [0 for i in range( no_of_sides )] def inputSides (self): self.sides = [float(input("Enter side "+ str (i+1)+" : ")) for i in range( self.n )] def dispSides (self): for i in range( self.n ): print ("Side",i+1,"is",self.sides[i]) 5 Class Inheritance class Triangle(Polygon): def __ init __( self): Polygon.__ init __(self, 3) def findArea (self ): a, b, c = self.sides # calculate the semi -perimeter s = (a + b + c) /2 area = (s*(s -a)*(s -b)*(s -c))**0.5 print('The area of the triangle is %0.2f' %area) t = Triangle() # Assume the values entered: 3 5 and 4 t.inputSides () # Output: Side 1 is 3.0 Side 2 is 5.0 Side 3 is 4.0 t.dispSides () # Output: The area of the triangle is 6.00 t.findArea () 6 Python Operator Overloading class Point: def __ init __(self, x=0, y=0): self.x = x self.y = y p1 = Point(1, 2) p2 = Point(2, 3) print(p1+p2) Output: Traceback (most recent call last): File "", line 9, in print(p1+p2 ) TypeError : unsupported operand type(s) for +: 'Point' and 'Point' 7 Python Special Functions • Begin and end with double underscore __ • __ str __: print function calls it. class Point: def __ init __(self, x = 0, y = 0): self.x = x self.y = y def __ str __(self): return '(' + str (self.x ) + ',' + str (self.y ) + ')' p1 = Point(2, 3) print(p1) Output: (2, 3) 8 Overloading the + operator class Point: def __ init __(self, x=0, y=0): self.x = x self.y = y def __ str __(self): return '(' + str (self.x ) + ',' + str (self.y ) + ')' def __add__(self, other): x = self.x + other.x y = self.y + other.y return Point(x, y ) p1 = Point(1, 2) p2 = Point(2, 3) print (p1+p2) Output: (3, 5) 9 Operator overloading 10 They are special methods with fixed names. Overloading Comparison Operators class Point: def __ init __(self, x=0, y=0): self.x = x self.y = y def __ str __(self): return '(' + str (x) + ',' + str (y) + ')' def __ lt__(self, other): self_mag = ( self.x ** 2) + ( self.y ** 2) other_mag = ( other.x ** 2) + ( other.y ** 2) return self_mag < other_mag p1 = Point(1,1) p2 = Point( -2, -3) # use less than print(p1