Class & Object in Python
MUKESH KUMAR
AGENDA
✓ Introduction to Classes & Objects
✓ Inbuilt Classes in Python
✓ Methods & Attributes
✓ Python Constructors
✓ Instance Variables & Class Variables
✓ Summary & Key Takeaways
What are Classes and Objects?
• A class is a blueprint or template for creating objects. It
defines attributes (data) and methods (functions) that
describe the behavior of the object.
• An object is an instance of a class that has actual values
assigned to its properties and can perform actions defined
by its methods.
Real-world Analogy for Better
Understanding
Consider a Car Factory:
•The class represents a blueprint for making cars.
•Each car produced from this blueprint is an object.
•Every car has common attributes (e.g., brand, model, color)
but different values.
How to Create a User Class?
• A User Class in Python refers to a class that is defined by
the programmer to model real-world entities or concepts.
• Syntax of class creation:
Class Example
CREATING A CLASS & OBJECT
Structure of a class
A class consists of:
• Class Definition: Using the class keyword.
• Attributes (Instance Variables): Data stored in an object.
• Methods (Functions within the Class): Define behavior of the objects.
• Constructor (__init__ Method): Special method to initialize instance
variables.
• Object Creation: Instantiating an instance of the class.
Class Example
Creating an Object of the Class
• To create an object, we use the class name followed by
parentheses, passing required arguments (if any) to the
constructor.
Key Points:
• Objects are created using the class name followed by
parentheses.
• We can create multiple objects from a single class.
• Methods allow objects to perform actions.
• Attributes store individual object data.
Attributes and Methods
• Attributes are variables that hold data related to an object,
representing its properties or characteristics
• Methods are functions associated with an object or class
that perform specific actions on that object
• Methods and attributes are fundamental to object-oriented
programming (OOP), allowing objects to have their own
data and behaviors, which helps in modeling real-world
entities
Attributes
• Attributes are variables that belong to an object and store
information about its state.
– They are defined within a class definition using the syntax
attr_name = attr_value.
– To access an attribute, you first create an instance of the class and
then use the dot (.) operator: instance_name.attr_name.
– Attributes can be class attributes (shared by all instances) or
instance attributes (unique to each instance).
Methods
• Methods are functions defined within a class that define
the actions an object can perform.
• They are defined within a class using the syntax:
– where method_name is the name of the method, self refers to the instance
of the object, and parameters are the arguments required by the method
Methods
• To call a method, you use the dot (.) operator on an
instance of the class: instance_name.method_name().
• Each method belongs to an object, such that calling
method(self) is equivalent to calling method(object)
Method Example
• In this example, name and color are attributes of the Cat class, while
meow() is a method1.
• The attributes store the cat's name and color, and the method makes
the cat meow
What is Meant by Inbuilt Class?
•Examples: list, dict, set, tuple, str, etc.
INIT() METHOD
Understanding __init__ Method
•It is called a constructor in Python
•Example
Python Constructor
• A constructor is a special method within a class that
automatically initializes a new object when it is created.
• The constructor's main role is to assign values to the data
members of the class.
• Python uses a method called __init__() to achieve this.
• When a new object is created, the constructor is automatically
called. If a class doesn't have a defined constructor, Python
automatically creates a default constructor
Rules of Python Constructor
• It starts with the def keyword, like all other functions in
Python.
• It is followed by the word init, which is prefixed and
suffixed with double underscores with a pair of brackets,
i.e., __init__().
• It takes an argument called self, assigning values to the
variables.
Constructor Types
Types of constructors
Default Parameterized
Constructor Constructor
Default Constructor
• This constructor does not accept any arguments other than
self.
• When a class doesn't explicitly define a constructor,
Python provides a default one.
Default Constructor Example
• In this example, each object declared from the Employee
class will have the same default values for the instance
variables name and age.
Parameterized Constructor
• This constructor accepts one or more arguments, allowing instance
variables to be initialized with specific values upon object creation.
• In this case, the __init__ method is a parameterized constructor that
sets the name and age attributes of the Person object to the provided
values
SELF()
• In Python, self is a special parameter used in class methods
to refer to the instance of the class.
• It allows you to access and modify the attributes and
methods of that instance.
• When you define a method within a class, the first
parameter is conventionally named self.
• Although you can technically use any name, sticking to self
enhances readability and aligns with Python's coding
conventions.
How self() works
• Instance Reference: self represents the instance of the
class. When a method is called on an object, Python
automatically passes the object itself as the first argument
to the method.
• Accessing Attributes: You use self to access and modify
instance variables. This distinguishes them from local
variables.
Example
• In this case, self.name refers to the instance variable name,
allowing each Dog instance to have its unique name
Why is self() needed
• Without self, methods wouldn't know which instance's data
to operate on, leading to errors.
• self ensures that changes to an attribute affect the instance
and not just a temporary variable.
Instance Variables
• Variables owned by the instances of a class, meaning each
instance has its own copy.
• They are defined within methods, particularly the
constructor (__init__).
• Instance variables are accessed using the instance name
(e.g., instance_name.variable_name).
Instance Variables
• Changes to an instance variable only affect that specific
instance and do not propagate to other instances.
• They are used to store data that is unique to each object.
Class Variables
• Variables defined within a class are shared among all
instances of that class.
• They reside at the class level and are typically placed
directly under the class header, before any methods.
• Class variables are accessed using the class name itself (e.g.,
ClassName.variable_name) or through an instance of the
class (e.g., instance_name.variable_name).
Class Variables
• Modifying a class variable affects all instances of the class.
• They are useful for storing values that should be consistent
across all instances or for initializing variables.
• They can also be used to keep track of the number of
instances created.
• Class variables can be of any data type available in Python.
Example
Summary
• Recap of all topics
• Key takeaways