Python: Classes and Objects

Table of contents

No heading

No headings in the article.

Object-oriented programming is a paradigm or methodology to solve a problem by creating objects and functions. Python is also one language that supports object-oriented programming as it contains classes and objects. So, let's dive into the concept of classes and objects in python.

What is Class?

  • A class is a collection of objects, it is not a real-world entity it is a blueprint and it does not occupy memory.
  • For example, Bike is a class
class Bike:
       pass                        #empty class

The pass statements tell that the particular class is empty for now.

  • A python class is a group of attributes and methods.
  • Attributes are represented by a variable that contains data.
  • Whereas, methods are a set of codes that perform a particular task. It is similar to function.

What are objects?

  • An object is the instance of the class, it is the real-world entity and it occupies the memory.
  • Each time you create an object of a class each variable is defined in the class created.

Syntax - object_name=classname() object_name=classname(arg)

  • Objects consist of the following :
  1. Identity- Unique name or identity

  2. State/Attribute- properties like name, color, price etc

  3. Behavior - Methods like dancing, singing, study, etc

Let's dive into an example :

Here we will consider a student class where

Class - Bike Identity - Honda Shine (Bike name) State/Attribute- Color, Price etc

How to create a Class?

The class keyword is used to create a class.

And object represents the base class name where all classes are derived in python.

_ init _()- Method is used to initialize the variables. This is the special method it runs when the object of the class is created.

Self-Self is the variable that refers to the current class instance/object. Self knows the memory location of the object so we can access the variable and method of that object.

Pastel Minimalist Self Care 3 Venn Diagram Graph (5).png

Now, let's look at the code part

Here the student class is created which contains an attribute which is roll number and methods are def_init() and show_rollNo()

# A class with one class attribute
class Bike:
    # class attribute
    wheels = 2
    # initializer with instance attributes
    def __init__(self, color, style):
        self.color = color
        self.style = style

Some rules while creating the class :

  1. The class name can be any valid identifier.
  2. It cant be a Python reserved word.
  3. A valid class name starts with a letter, followed by any number of letters, or the umber of underscores.
  4. A class name generally starts with capital letters.

How to create objects?

Pastel Minimalist Self Care 3 Venn Diagram Graph (6).png

Example : Each time you create an object of class a copy of each variable is defined in the class is created.

class Bike:
    # class attribute
    wheels = 2
    # initializer with instance attributes
    def __init__(self, color, price):
        self.color = color
        self.style = style
b = Bike('Black', '74000')

Working:

A block of memory is allocated in heap. The size is allocated to be decided from the attributes and methods available in the class (Student). After allocating the memory block, the special method _ init _ is called internally which stores the data into variables. The allocated memory location address of the instance is returned to the object (SAM) The memory location is passed to the self.

If we want to modify Attributes

# Access and modify attributes of an object
class Bike:
    # class attribute
    wheels = 2
    # initializer with instance attributes
    def __init__(self, color, style):
        self.color = color
        self.model = model
b= Car('Black', 'Honda Shine')
# Access attributes
print(b.model)
# Prints Honda Shine
print(b.color)
# Prints Black
# Modify attribute
b.style = 'Bajaj Pulsar 150'
print(b.style)
# Prints Bajaj Pulsar 150

Difference between classes and objects

Blue and Brown Plot Graphic Organizer (1).png

Did you find this article valuable?

Support Samruddhi Wasu by becoming a sponsor. Any amount is appreciated!