Python Programming Unit III
Python Programming Unit III
Classes
Classes and objects are the two main aspects of object
oriented programming.
In fact, a class is the basic building block in python
A class creates a new type and object is an instance (or
variable) of the class.
Classes provides a blueprint or a template using which
objects are created.
In fact, in python, everything is an object or an instance of
some class.
All integers variables that we define in our program are
actually instances of class int.
All string variables are objects of class string.
Python Classes and Objects
Classes
We had used string methods using the variable name
followed by the dot operator and the method name.
We can find out the type of any object using the type()
function.
From the syntax we see that class definition is quite similar
to function definition.
It starts with a keyword class followed by the class_name
and a colon(:).
The statement in the definition can be any of these
sequential instructions, decision control statements, loop
statements and can even include function definition.
Variables defined in a class are called class variables.
Function defined in a class are called class methods.
Python Classes and Objects
Classes
Class variables and class methods are together known as
class members.
The class members can be accessed through class objects.
Class methods have access to all the data contained in the
instance of the object.
Class definitions can appear anywhere in a program, but
they are usually written near the beginning of the program,
after the import statements.
When a class definition is entered, a new namespace is
created, and used as the local scope.
Therefore, all assignments to local variables go into this new
namespace.
Python Classes and Objects
Classes
A class creates a new local namespace where all its
attributes(data and functions) are defined.
Once a class is define, the next jbb is to create an object(or
instance) of that class
The object can then access class variables and class
methods using the dot operator(.).
Object_name=class_name()
Creating an object or instance of a class is known as class
instantiation.
To crate a new object, call a class as if it were a function.
For accessing a class member through the class object is
Object_name.class_member_name
Python Classes and Objects
Classes
Syntax:
class class_name:
statement 1
statement 2
Example:
class ABC:
var=10
obj=ABC()
print(obj.var)
OUTPUT:
10
Python Classes and Objects
Classes
Example:
class ABC():
var=10
def display(self):
print(“In class method……”)
obj=ABC()
print(obj.var)
obj.display()
OUTPUT:
10
In class method……..
If you have a method which takes no arguments then you still
have to define the method to have a self argument
Python Classes and Objects
Classes:
Key points to remember:
The statements inside the class definition must be properly
indented.
A class that has no other statements should have a pass
statement at least.
Class methods or functions that begins with double
underscore(__) are special functions with a predifined and a
special meaning.
Python Classes and Objects
Classes:
Constructor:
Example:
The __init__() method is automatically executed when an
object of a class is created.
class ABC():
def __init__(self,val):
print(“In class method…”)
self.val=val
print(“The value is:”,val)
obj=ABC(10)
OUTPUT:
In class method……
The value is : 10
Python Classes and Objects
Classes: Class Variables and Object Variables
If a class has n objects, then there will be n separate copies of
the object variables as each object will have its own object
variable.
The object variable is not shared between objects.
A change made to the object variable by one object will not be
reflected in other objects.
If a class has one class variable, then there will be one copy only
for that variable. All the objects of that class will share the class
variable.
Since there exists a single copy of the class variable, any change
made to the class variable by an object will be reflected in all other
objects.
Class variables and object variables are ordinary variables that
are bound to the class’s and object’s namespace respectively.
Python Classes and Objects
Classes:
Constructor:
Example:
Class ABC():
class_var=0
def __init__(self,var):
ABC.class_var+=1
self.var=var
print(“The object value is:”,var)
print(“The value of class variable
is:”,ABC.class_var)
Obj1=ABC(10)
Obj2=ABC(20)
Obj3=ABC(30)
Python Classes and Objects
Classes:
Constructor:
OUTPUT:
The object value is: 10
The value of class variable is: 1
The object value is: 20
The value of class variable is: 2
The object value is: 30
The value of class variable is: 3