Skip to main content

Advance Object Oriented programming in Python


The following program will illustrates the method overriding concept onf OOP. Here parent and child classes have defined with the same method name and signature. According to context the method would be executed. The actual meaning of overriding means ignoring the parent method when both have same method signature, and executing the derived class method.

===== METHOD OVERRIDING SAMPLE PROGRAM =====

class FirstClass:            #define the super class  
 def setdata(self, value):     # define methods
  self.data = value # ‘self’ refers to an instance 
 def display(self):  
  print self.data

class SecondClass(FirstClass):  # inherits from FirstClass
 def display(self):  # redefines display
  print 'Current Data = %s'  % self.data
x=FirstClass()  # instance of FirstClass
y=SecondClass()  # instance of SecondClass
x.setdata('Before Method Overloading')
y.setdata('After Method Overloading')
x.display()
y.display()

===== Static and Class Method Sample Programs =====

class Students(object):
 total = 0
 def status():
  print '\n Total Number of studetns is :', Students.total
 status= staticmethod(status)
 def __init__(self, name):
  self.name= name
  Students.total+=1
print ‘Before Creating instance: ‘, Students.total
student1=Students('Guido')
student2=Students('Van')
student3=Students('Rossum')

Students.status() # Accessing the class attribute through direct class name 
student1.status() # Accessing the class attribute through an object
class Spam:
 numinstances = 0
 def count(cls):
  cls.numinstances +=1
 def __init__(self):
  self.count()
 count=classmethod(count)     # Converts the count function to class method
class Sub(Spam):
 numinstances = 0
class Other(Spam):
 numinstances = 0
S= Spam()
y1,y2=Sub(),Sub()
z1,z2,z3=Other(),Other(),Other()
print S.numinstances, y1.numinstances,z1.numinstances
print Spam.numinstances, Sub.numinstances,Other.numinstances

Comments

Popular posts from this blog

Python Sets and Dictionaries

Python Sets Python Maps examples A mapping object maps hashable values to arbitrary objects. Mappings are mutable objects. There is currently only one standard mapping type, the dictionary. Dictionaries consist of pairs (called items) of keys and their corresponding values. Dictionaries can be created by placing a comma-separated list of key: value pairs within curly braces Keys are unique within a dictionary while values may not be. The values of a dictionary can be of any type, but the keys must be of an immutable data type such as strings, numbers, or tuples. Python Set Operations and methods Python Set methods continueed >>> SET={'new','old','list','new'} >>> SET set(['new', 'old', 'list']) Lets play with set operations, it is more like school math here... >>> s={1,2,3,4} >>> k={3,4,5,6} >>> len(s) 4 >>> len(k) 4 >>> 1 in s True >>...

AWS Auto Scaling using Python Boto3

Auto Scaling Group in AWS configure using Python Boto3  What is Auto Scaling means?  This is key capability or power of Cloud Computing Engineers believe in their skills on scaling abilities.  Amazon EC2 Auto Scaling helps to maintain application availability and lets automatically add or remove EC2 instances using scaling policies that we define.  There are 2 types of scaling policies : Dynamic or predictive. These scaling policies let us add or remove EC2 instances capacity to service established or real-time demand patterns. It contains various steps involved in Auto Scaling process using Python Boto3 we will explore every step that accumulate to form a complete automation solution for a DevOps project. ASG Groups associated with ELB and EC2 instances   Understanding AWS Auto scaling configuration steps Check any running instances Create launch configuration Configure ASG for Auto scaling Verify the configuration Disable Auto Scaling In order to setup A...

Importing modules

What is Python Module? When a Python shell starts it only has access to a basic python functions (“int”, “dict”, “len”, “sum”, “range”, ...) “Modules” that contain additional functionality more methods, more reusability and more sharability. Use “import” keyword to tell the Python shell to load a module. import os, sys Namespaces are one honking great idea -- let's do more of those! (Zen of Python) import this Why importing modules in Python? The major advantage comes when you split your program into several files for easier maintenance. How to import modules in Python A module can be imported by another program to make use of its functionality. This is how we can use the Python standard library as well You can import multiple modules. import module1[, module2[,... moduleN] Normally after module import we can use its containing functions by calling them with refering with the module name followed by period or dot(.) and the function/procedure name. import...