Python Object Oriented2
Python Object Oriented2
Class variable í A variable that is shared by all instances of a The class has a documentation string, which can be accessed via
class. Class variables are defined within a class but outside any of ClassName.__doc__.
the class's methods. Class variables are not used as frequently as The class_suite consists of all the component statements defining
instance variables are. class members, data attributes and functions.
'Common base class for all employees' You access the object's attributes using the dot operator with object.
empCount = 0 Class variable would be accessed using class name as follows í
emp1.displayEmployee()
def __init__(self, name, salary):
emp2.displayEmployee()
self.name = name
print "Total Employee %d" % Employee.empCount
self.salary = salary
Employee.empCount += 1 Now, putting all the concepts together í
#!/usr/bin/python
def displayCount(self):
print "Total Employee %d" % Employee.empCount class Employee:
'Common base class for all employees'
def displayEmployee(self): empCount = 0
print "Name : ", self.name, ", Salary: ",
self.salary def __init__(self, name, salary):
The variable empCount is a class variable whose value is shared self.name = name
among all instances of a this class. This can be accessed as self.salary = salary
You declare other class methods like normal functions with the def displayEmployee(self):
exception that the first argument to each method is self. Python print "Name : ", self.name, ", Salary: ",
adds the self argument to the list for you; you do not need to self.salary
include it when you call the methods.
"This would create first object of Employee class"
Creating Instance Objects emp1 = Employee("Zara", 2000)
"This would create second object of Employee
To create instances of a class, you call the class using class name
class"
and pass in whatever arguments its __init__ method accepts.
emp2 = Employee("Manni", 5000)
"This would create first object of Employee class" emp1.displayEmployee()
emp1 = Employee("Zara", 2000) emp2.displayEmployee()
"This would create second object of Employee print "Total Employee %d" % Employee.empCount
class"
When the above code is executed, it produces the following result í
emp2 = Employee("Manni", 5000)
Name : Zara ,Salary: 2000
Accessing Attributes Name : Manni ,Salary: 5000
Total Employee 2 For the above class let us try to access all these attributes í
You can add, remove, or modify attributes of classes and objects at #!/usr/bin/python
any time í
class Employee:
emp1.age = 7 # Add an 'age' attribute.
'Common base class for all employees'
emp1.age = 8 # Modify 'age' attribute.
empCount = 0
del emp1.age # Delete 'age' attribute.
Instead of using the normal statements to access attributes, you can def __init__(self, name, salary):
use the following functions í self.name = name
The getattr(obj, name[, default]) í to access the attribute of self.salary = salary
object. Employee.empCount += 1
Derived classes are declared much like their parent class; however, c.getAttr() # again call parent's method
a list of base classes to inherit from is given after the class name í
When the above code is executed, it produces the following result í
class SubClassName (ParentClass1[, ParentClass2,
Calling child constructor
...]):
Calling child method
'Optional class documentation string'
Calling parent method
class_suite
Parent attribute : 200
Example Similar way, you can drive a class from multiple parent classes as
follows í
#!/usr/bin/python
class A: # define your class A
class Parent: # define parent class .....
parentAttr = 100
def __init__(self): class B: # define your class B
print "Calling parent constructor" .....
def getAttr(self): given subclass sub is indeed a subclass of the superclass sup.
print "Parent attribute :", The isinstance(obj, Class) boolean function returns true if obj is
Parent.parentAttr an instance of class Class or is an instance of a subclass of Class
Example
Base Overloading Methods
#!/usr/bin/python
Following table lists some generic functionality that you can
override in your own classes í
class Vector:
Sr.No. Method, Description & Sample Call def __init__(self, a, b):
__init__ ( self [,args...] ) self.a = a
self.b = b
1 Constructor (with any optional arguments)
Example
#!/usr/bin/python
class JustCounter:
__secretCount = 0
def count(self):
self.__secretCount += 1
print self.__secretCount
counter = JustCounter()
counter.count()
counter.count()
print counter.__secretCount
1
2
Traceback (most recent call last):
File "test.py", line 12, in <module>
print counter.__secretCount
AttributeError: JustCounter instance has no
attribute '__secretCount'
.........................
print counter._JustCounter__secretCount
1
2