├── README.md └── object_oriented_python ├── MyClass.py ├── ShapeClass.py ├── StudentClass.py └── class cal.py /README.md: -------------------------------------------------------------------------------- 1 | # object_oriented_python 2 | 3 | This is simple example for understanding object oriented in python. 4 | -------------------------------------------------------------------------------- /object_oriented_python/MyClass.py: -------------------------------------------------------------------------------- 1 | class MyClass: 2 | MyVar = 0 3 | 4 | 5 | myInstance = MyClass() 6 | print(myInstance.MyVar) 7 | myInstance.MyVar = 100 8 | print(myInstance.MyVar) 9 | 10 | -------------------------------------------------------------------------------- /object_oriented_python/ShapeClass.py: -------------------------------------------------------------------------------- 1 | class Shape: 2 | size = 2 3 | def __init__(self, name, color): 4 | self.name = name 5 | self.color = color 6 | 7 | 8 | shape1 = Shape('Circle','Yellow') 9 | shape2 = Shape('Square','Green') 10 | 11 | print(shape1.name) 12 | print(shape2.color) 13 | 14 | shape1.color = 'blue' 15 | shape1.radius = 20 16 | 17 | 18 | print(shape1.color) 19 | print(shape1.radius) 20 | print(shape1.name) 21 | 22 | print(hasattr(shape2, 'radius')) # Returns true if 'radius' attribute exists 23 | print(getattr(shape1, 'radius')) # Returns value of 'radius' attribute 24 | setattr(shape2, 'radius', 8) # Set attribute 'radius' at 8 25 | print(shape2.radius) 26 | delattr(shape1, 'color') # Delete attribute 'color' 27 | #print(shape1.color) 28 | 29 | del shape1.name 30 | print(shape1.name) 31 | 32 | 33 | -------------------------------------------------------------------------------- /object_oriented_python/StudentClass.py: -------------------------------------------------------------------------------- 1 | class Student: 2 | 'Common base class for all students' 3 | count = 0 4 | def __init__(self, name, family): 5 | self.name = name 6 | self.family = family 7 | Student.count += 1; 8 | 9 | def displayCount(self): 10 | print("Total Student : ", Student.count) 11 | 12 | def displayStudent(self): 13 | print("Name : ", self.name, ", family: ", self.family) 14 | 15 | print("Student.__doc__:", Student.__doc__) 16 | print("Student.__name__:", Student.__name__) 17 | print("Student.__module__:", Student.__module__) 18 | print("Student.__bases__:", Student.__bases__) 19 | print("Student.__dict__:", Student.__dict__) 20 | -------------------------------------------------------------------------------- /object_oriented_python/class cal.py: -------------------------------------------------------------------------------- 1 | class calcu: 2 | def __init__(self,input1,input2): 3 | self.a=input1 4 | self.b=input2 5 | def sum1(self): 6 | return self.a+self.b 7 | 8 | class calcu2(calcu): 9 | def multi(self): 10 | return self.a*self.b#+self.sum1() 11 | 12 | 13 | mycal=calcu(30,5) 14 | print(mycal.sum1()) 15 | 16 | mycal2=calcu2(30,20) 17 | print(mycal2.sum1()) 18 | print(mycal2.multi()) 19 | --------------------------------------------------------------------------------