DEV Community

Faith Mueni Kilonzi
Faith Mueni Kilonzi

Posted on

What you need to know about OOP in Python

Understanding Python Classes
Python is an Object-oriented programming language. This means that everything is an object in python, with properties and methods which define the object.
A class in python, is a blueprint or a prototype for creating objects. For instance, if you want to build a house, you will first need a blueprint containing basic properties (characteristics) and methods(functionalities) of what a general house would look like. Using the class, you can now create instances (different examples) of the same house. A class is therefore an object constructor that defines the object.
For example, to create an object Student, we represent a student with properties such as name, course, age, student number, etc., and behaviors such as reading, attending class, programming, etc. Another object could be an animal whose properties include number of legs, habitat, name, type of food, etc. and functionalities such as walking, eating, living, etc.
Simply put, OOP is an approach used to model real-life entities which have data associated with them and can perform certain functions.

         Creating the class
Enter fullscreen mode Exit fullscreen mode

Use a class keyword.
Class Animal(object):

The python constructor: init ()

Every python class starts with a function called init () which is used to initialize the object’s attributes by assigning to them their default values. This function has at least one argument(parameter) called self which references the object itself e.g. Animal.

class Animal:

# Initializer / constructor
def __init__ (self, name, habitat):
    self.name = name        
    self. Habitat = habitat
Enter fullscreen mode Exit fullscreen mode

Instantiating Objects
To instantiate an object means to create a new, unique instance of a class. You call the class name by giving it real values to model an example of the class.
class Animal:

# Initializer / constructor
def init (self, name, habitat):
self.name = name

self. habitat = habitat
Enter fullscreen mode Exit fullscreen mode




Instantiate the Animal object

Animal_object = Animal(“Dog”, “home”);

Access an instance attributes

print(animal_object.name)
print(animal_object.habitat)

Instance Methods
Instance methods are functions that belong to the object and are defined inside a class to get contents of an instance. They are used to perform operations with the object attributes. For example, a getName method is used to get the name of the object instantiated by the class. Like the init method, the first argument is always self.

class Animal:

# Initializer / constructor
def init(self, name, habitat):
self.name = name

self.habitat = habitat
Enter fullscreen mode Exit fullscreen mode




instance method


def getName(self, name):
return self.name
Enter fullscreen mode Exit fullscreen mode




instance method


def description(self):
return "my name is a {} and I live in the {}".format(self.name, self. habitat)
Enter fullscreen mode Exit fullscreen mode




Instantiate the Animal object

animal_object = Animal(“Dog”, “home”);

Calling an instance method

animal_object.getName(“Puppy”)
animal_object.description()

Save the animal.py file and run it. The output you see should be:
Puppy
My name is a dog and I live in the home.
Modify Object Properties
You can change the value of the object properties.
To change the name of the object:
animal_object.name= ‘Mikey’

this sets the name from ‘dog’ to ‘Mikey’

To delete the property attribute:

del animal_object.name

Top comments (3)

Collapse
 
joblessprogrammer profile image
sushil sarwade • Edited

Add @property to your article so that it is everything at one point

Collapse
 
global_codess profile image
Faith Mueni Kilonzi

Well noted

Collapse
 
vincenttommi profile image
Tommi k Vincent

educative