NED University of Engineering and Technology
Department of Computer and Information Systems Engineering
CLASSES AND OBJECT IN PYTHON
CS-116 Object Oriented Programming
Spring 2025
Ms. Fauzia Yasir
CS 116 - Lecture 03
Classes and Object in Python
Last Time . . .
Classes are blueprints/code snippets which can be used to instantiate objects
We can instantiate as many objects from a class blueprint as we need
Each instantiated object will have its own set of attributes and behaviours
Encapsulation and data hiding, inheritance, polymorphism and association constitute the main
strengths of object oriented programming paradigm
Data and behaviour can be encapsulated in a single object, enabling it to hide whatever it
wants from other objects
2
CS 116 - Lecture 03
Classes and Object in Python
Today’s Session . . .
3
CS 116 - Lecture 03
Classes and Object in Python
Python Classes and Objects
• Classes are created using class keyword.
• Attributes are variables defined inside the class and
represent the properties of the class. Attributes can be
accessed using the dot . operator (e.g.,
MyClass.my_attribute).
4
CS 116 - Lecture 03
Classes and Object in Python
Defining Classes in Python - Syntax
5
CS 116 - Lecture 03
Classes and Object in Python
Defining Classes in Python - Example
Define a class Dog with class attribute sound
# define a class
class Dog:
sound = "bark" # class
attribute
6
CS 116 - Lecture 03
Classes and Object in Python
Defining Classes in Python - Example
Define a class Dog with class attribute sound Output:
Class Attribute of Dog:
# define a class bark
class Dog:
sound = "bark" # class attribute
An Object is an instance of a Class. It represents a specific
implementation of the class and holds its own data.
Now, let’s create an instance from Dog class.
# Create an instance from the class
dog1 = Dog()
# Access the class attribute
print(‘Class Attribute of Dog:’+dog1.sound)
7
CS 116 - Lecture 03
Classes and Object in Python
Defining Classes in Python - Example
Define a class Dog with class attribute sound
# define a class
class Dog:
sound = "bark" # class attribute
# Create an object from the class
dog1 = Dog()
# Access the class attribute
print(‘Class Attribute of Dog:’+dog1.sound)
sound attribute is a class attribute. It is shared across all instances of Dog class, so can be directly
accessed through instance dog1.
8
CS 116 - Lecture 03
Classes and Object in Python
Defining Classes in Python - Example
Define a class Dog with class attribute sound
# define a class
class Dog:
sound = "bark" # class attribute
# Create an object from the class
dog1 = Dog()
# Access the class attribute
print(‘Class Attribute of Dog:’+dog1.sound)
sound attribute is a class attribute. It is shared across all instances of Dog class, so can be
directly accessed through instance dog1.
Class attribute :
• These are the variables that are shared across all instances of a class.
• It is defined at the class level, outside any methods.
• All objects of the class share the same value for a class variable unless
9
explicitly overridden in an object.
CS 116 - Lecture 03
Classes and Object in Python
Defining Classes in Python - Example
Self Parameter
self parameter is a reference to the current instance of the class. It allows us to
access the attributes and methods of the object.
# define a class
class Dog:
sound = "bark" # class attribute
#defining setter methods
def setName (self, n):
self.Name = n
def setAge (self, a):
self.Age = a
10
CS 116 - Lecture 03
Classes and Object in Python
Defining Classes in Python - Example
Self Parameter
self parameter is a reference to the current instance of the class. It allows us to
access the attributes and methods of the object.
# define a class
class Dog:
sound = "bark" # class attribute
#defining setter methods
Name and Age are Instance attributes.
def setName (self, n):
Instance Variables/Attributes
self.Name = n Variables that are unique to each instance (object) of a
def setAge (self, a): class. These are defined within __init__ method or other
self.Age = a instance methods. Each object maintains its own copy of
instance variables, independent of other objects.
dog1 = Dog()
print(‘Class Attribute of Dog:’+dog1.sound)
dog1. setName(‘Harry’)
dog1.setAge(3) 11
CS 116 - Lecture 03
Classes and Object in Python
Defining Classes in Python - Example
using self parameter to access class and instance attribute within class
definition
# define a class
class Dog:
sound = "bark" # class attribute
#defining setter methods
def setName (self, n):
self.Name = n
def setAge (self, a):
self.Age = a
def info (self):
print(‘My name is’+ self.Name+’ and my age is’+ self.Age+’and I
can’+ self.sound)
12
CS 116 - Lecture 03
Classes and Object in Python
Defining Classes in Python - Example
using self parameter to access class and instance attribute within class
definition
dog1 = Dog()
print(‘Class Attribute of Dog:’+dog1.sound)
# define a class print(dog1.Name)
class Dog: print(dog1.Age)
sound = "bark" # class attribute dog1. setName(‘Harry’)
#defining setter methods dog1.setAge(3)
def setName (self, n): dog1.info()
self.Name = n print(dog1.Name)
def setAge (self, a): print(dog1.Age)
self.Age = a
def info (self):
print(‘My name is’+ self.Name+’ and my age is’+ self.Age+’and I can’+ self.sound)
Output:
My name is Harry and my age is 3 and I can bark
Harry
3 13
CS 116 - Lecture 03
Classes and Object in Python
Defining Classes in Python - Example
creating instance attribute through instance
dog1 = Dog()
dog2 = Dog()
dog1. setName(‘Harry’)
# define a class
dog1.setAge(3)
class Dog:
dog1.info()
sound = "bark" # class attribute
dog2.info()
#defining setter methods
dog2.Name=‘Happy’
def setName (self, n):
dog2.Age=5
self.Name = n
dog2.info()
def setAge (self, a):
self.Age = a
def info (self):
print(‘My name is’+ self.Name+’ and my age is’+ self.Age+’and I can’+ self.sound)
14
CS 116 - Lecture 03
Classes and Object in Python
Defining Classes in Python - Example
defining getter method
# define a class
class Dog:
sound = "bark" # class attribute
#defining setter methods
def setName (self, n):
self.Name = n
def setAge (self, a):
self.Age = a
#defining getter methods
def getName (self):
return self.Name
def getAge (self):
return self.Age
def info (self):
print(‘My name is’+ self.Name+’ and my age is’+ self.Age+’and I can’+ self.sound)
15
CS 116 - Lecture 03
Classes and Object in Python
Defining Classes in Python - Example
defining getter method
# define a class
dog1 = Dog()
class Dog:
print(dog1.getName())
sound = "bark" # class attribute
dog1. setName(‘Harry’)
#defining setter methods
print(dog1.getName())
def setName (self, n):
dog1.info()
self.Name = n
dog1.setAge(3)
def setAge (self, a):
dog1.info()
self.Age = a
#defining getter methods
def getName (self):
return self.Name
def getAge (self):
return self.Age
def info (self):
print(‘My name is’+ self. getName()+’ and my age is’+ self. getAge()+’and I can’+
self.sound)
16
CS 116 - Lecture 03
Classes and Object in Python
CS 116 - Lecture 03
Classes and Object in Python
CS 116 - Lecture 03
Classes and Object in Python
CS 116 - Lecture 03
Classes and Object in Python
A Quick Recap . . .
Learned syntax for defining classes and instances.