Python: Classes

Python is an “object-oriented programming language.” Almost all the code is implemented using a special construct called classes.
Programmers use classes to keep related things together. This is done using the keyword “class“.

What is a Class?

  • A Class is a model
  • A class is a blueprint(map) of anything
  • A class is a template
  • A class may also be defined as something that can be followed to
  • create objects and instances

Writing a Class

Python uses the keyword “class” to define a class

Class Car():
# body of class Car

Writing a Class

Python uses the keyword “class” to define a class

Class Car():
      # body of class Car

What does the actual class hold??

A class may hold attributes (variables)
A class may hold behaviours (functions)
Example:
A car’s colour, model, and seating capacity are attributes of the car.
A car can run, stop, speed_up, and speed_down are behaviours of a car

Classes: Creating an instance

Almost everything in Python is an object, with its properties and methods.
Objects are made following its class means if an objects belongs to a class it must have been following the requirements set by the class

class Car():
     # define a class body
         # attributes and behaviours

# Creating objects/instance of Car class
    car1 = Car()
    car2 = Car()