Explore
Placement Prep

Edit

Edit

# Classes and Objects In Python | FACE Prep

Published on 09 Mar 2020

In previous articles, we have learnt various fundamental concepts of Python like Variables, Functions, Modules, Files, etc. Now it's time for us to learn Python OOPs concepts. Under the concept of OOPs, class and object are the important components that are used to implement the rest of the OOPs concepts. So in this article, let us explore more about classes and objects in Python.

For example, in the real world, there can be thousands of cars in use. All cars will have four wheels, an engine, steering, etc. Since each car was built from the same set of blueprints, they contain the same components. In object-oriented terms, we say the car as an object and blueprint of the car as a class.

But still, we are not sure of how classes and objects in Python work right? So, let us go ahead and learn about them in detail.

## Objects in Python

Objects are real-world entities. Each and every object will have certain attributes (properties) and behaviors (actions). For example, color, length, width of a car are attributes and actions like driving, applying brakes, etc can be referred to as behaviors. In order to define attributes, we use variables and to define behaviors, we use methods.

Note:In object-oriented terms, methods are similar to functions

## Classes in Python

A class is a design or a blueprint for which objects are created. Similar to how a single blueprint is used to design different brands of cars, a single class is used to create more than one object.

Since a class acts as a blueprint to create an object, we need to create a class before creating an object. So, let us see how to create a class.

## How To Create Classes In Python?

In Python, a class is created by using the keyword 'class'. Below is the syntax for creating the class.

Syntax
class class_name:


Example

class Car:

Note:Class names should follow the UpperCaseCamelCase convention.

Next, let us see how to create an object for the class 'Car'.

## Creating Objects In Python

An object created for a class inside the main() method is used to access variables and methods defined inside the class. Below is the syntax for creating the object.

Syntax
object_name = class_name()


Example

maruti = Car()

Similar to how height and width of the car are mentioned in the blueprint, we need to define variables, methods inside the class. So, let us define a variable and a method inside the class Car.

class Car:
length = '470cm' #variabledef drive(self): #method
print("I can drive at a speed of 60km/hr")


Now that we have defined the method 'drive' inside the class 'Car'. Next, let us see how to call the method 'drive' from the main() method.

## How To Call A Method Defined Inside A Class?

The method defined inside the class 'Car' can be called using the object 'maruti' as shown below.

#main() method
maruti = Car()
Car.drive(maruti)
Output:
I can drive at a speed of 60km/hr


We have passed one object as the argument to the method 'drive'. To accept and store the object 'maruti', we have written the keyword 'self' as a parameter in the method 'drive'.

Here, 'maruti' is one object created for the class 'Car'. Similarly, we can create more than one objects for the class 'Car'.

#main() method
maruti = Car()
audi = Car() #the second object created for the class 'Car'
Car.drive(maruti)
Car.drive(audi)
Output:
I can drive at a speed of 60km/hr
I can drive at a speed of 60km/hr


We can also call a method defined inside the class 'Car' using the objects 'maruti' and audi as shown below.

#main() method
maruti = Car()
audi = Car()
maruti.drive()
audi.drive()
Output:
I can drive at a speed of 60km/hr
I can drive at a speed of 60km/hr


Here, we are not passing any arguments. But by default, the Python interpreter takes 'maruti' and 'audi' as the arguments. However, the second way of calling the method is preferable.

We can also call the variable defined inside the class 'Car' as shown in the below example.

class Car:
length = '470cm'def drive(self):
print("I can drive at a speed of 60km/hr")

#main() method
maruti = Car()
print(maruti.length) #prints the value stored in the variable 'length'
maruti.drive() #call to the method named 'drive'
Output:
470cm
I can drive at a speed of 60km/hr


## Classes and Objects In Python FAQs

1) What are Classes in Python?

A class is a design or a blueprint for which objects are created.

2) What are objects in Python?

Objects are real-world entities. Each and every object will have certain attributes (properties) and behaviors (actions).