Sie sind auf Seite 1von 3

Python Scripting

Python – class and object

Referring official python documentation, it is to be noted that class mechanism in python is a


mixture of class mechanisms found in other object oriented languages like C++ and Modula-3.

#=====================================================================
# program to demonstrate class/objects
# # Filename -python_class_obj_1.py
#=====================================================================

class DemoClass:
"""A simple demonstration class"""
#class variable, shared by all instances
no_of_instances = 0
def __init__(self, name):
#name on the left and name on the right are different
if name:
self.name = name
else:
self.name = "default"
DemoClass.no_of_instances += 1

def die(self):
print self.name, "! getting destroyed"
DemoClass.no_of_instances -= 1

def sayhi(self):
print "Hi!! I am ", self.name

#=====================================================================
# main function getting defined below
#=====================================================================

def main():
"""main function."""

#printing the documentation string of the class


print DemoClass.__doc__

#creating new instances of the class and assigning it to


#local variables x and y
x = DemoClass("object1")
y = DemoClass("object2")
z = DemoClass("")
print y.no_of_instances
y.sayhi()
x.sayhi()
z.sayhi()
x.die()
print y.no_of_instances
# Entry point of the script
if __name__ == "__main__":
main()

########################################################################
#
Points to note:

a) When a class definition defines an __init__ method, class instantiation automatically


invokes this method.
b) The only operations understood by instance objects are attribute references. There are two
kinds of valid attribute names, data attributes and methods.
c) Data attributes need not be declared; like local variables, they come into existence when
they are first assigned to.
d) The other kind of instance attribute reference is a method. A method is a function that
“belongs to” an object.

Let us see another simple example. This helps us to understand inheritance.

#=====================================================================
# program to demonstrate inheritence
# # Filename -python_class_obj_1.py
#=====================================================================

class SchoolMember:
"""Represents any school member."""
def __init__(self, name, age):
self.name = name
self.age = age
print "Initialized SchoolMember", self.name
def tell(self):
"""Tell my details."""
print "Name: %s Age: %d" %(self.name, self.age)

class Teacher(SchoolMember):
"""Represents a teacher."""
def __init__(self, name, age, salary):
SchoolMember.__init__(self, name, age)
self.salary = salary
print "Initialized Teacher", self.name
def tell(self):
SchoolMember.tell(self)
print "Salary: %d" %(self.salary)

class Student(SchoolMember):
"""Represents a student."""
def __init__(self, name, age, marks):
SchoolMember.__init__(self, name, age)
self.marks = marks
print "Initialized Student ", self.name
def tell(self):
SchoolMember.tell(self)
print "Marks: %d" %(self.marks)
#====================================================================
# main function getting defined below
#====================================================================

def main():
"""main function."""
t = Teacher('anurag', 40, 30000)
s = Student('avishek', 25, 75)
print "####################################"
members = [t, s]
for member in members:
member.tell() # works for both Teachers and Students

# Entry point of the script


if __name__ == "__main__":
main()

########################################################################
#

References
a) https://docs.python.org/2/tutorial/classes.html#tut-object
b) http://www.swaroopch.com/notes/python/