Classes can be looked upon as a mechanism that allows programmers to define new types. An abstract data type is a set of objects and the operations on those objects. These are bound together so that one can pass an object from one part of a program to another and in doing so provide access not only to the data attributes of the object but also to operations that make it easy to manipulate that data.
The int, string, float, list, dict are all built in types using classes.
The process of creating an object of type class is callled instantion of class (calling a class object). Some classes can be be xreated with a specific initial state. This is done by a special method __init__. As we see in code below. We create a class calulator which has instances value1,value2and operand.
Example :
class calculator():
def __init__(self,value1 , value2, operand):
self.value1 = int(value1)
self.value2 = int(value2)
self.operand = operand
def calculate (self):
if self.operand == '+' :
return self.value1 + self.value2
if self.operand == '*':
return self.value1 * self.value2
if self.operand == '-' :
return self.value1 - self.value2
if self.operand == '/' :
try:
self.value1 / self.value2
except ZeroDivisionError:
print "Zero division error"
else:
print "a"
return self.value1 / self.value2
Test cases :
r = calculator(6,7, '+')
print r.calculate()
r = calculator(3,7, '-')
print r.calculate()
r = calculator(6,0, '*')
print r.calculate()
r = calculator(9,0, '/')
print r.calculate()
r = calculator(9,8, '/')
print r.calculate()
The int, string, float, list, dict are all built in types using classes.
The process of creating an object of type class is callled instantion of class (calling a class object). Some classes can be be xreated with a specific initial state. This is done by a special method __init__. As we see in code below. We create a class calulator which has instances value1,value2and operand.
Example :
class calculator():
def __init__(self,value1 , value2, operand):
self.value1 = int(value1)
self.value2 = int(value2)
self.operand = operand
def calculate (self):
if self.operand == '+' :
return self.value1 + self.value2
if self.operand == '*':
return self.value1 * self.value2
if self.operand == '-' :
return self.value1 - self.value2
if self.operand == '/' :
try:
self.value1 / self.value2
except ZeroDivisionError:
print "Zero division error"
else:
print "a"
return self.value1 / self.value2
Test cases :
r = calculator(6,7, '+')
print r.calculate()
r = calculator(3,7, '-')
print r.calculate()
r = calculator(6,0, '*')
print r.calculate()
r = calculator(9,0, '/')
print r.calculate()
r = calculator(9,8, '/')
print r.calculate()
So r is an instance of class calculator.
The only operations understood by instance objects is attribute reference. There are two kind of attributes one data attributes and second method attributes.
Data attribute are variables defined in the class.
Method attributes are functions that a class object can do. These attributes are reffered using . name notation.
In the example above the class object has method function which is called once the instance object is created by command r.calculate .
The only operations understood by instance objects is attribute reference. There are two kind of attributes one data attributes and second method attributes.
Data attribute are variables defined in the class.
Method attributes are functions that a class object can do. These attributes are reffered using . name notation.
In the example above the class object has method function which is called once the instance object is created by command r.calculate .
No comments:
Post a Comment