Object-oriented programming in python.

keshav
Nerd For Tech
Published in
4 min readJun 11, 2021

--

Object-oriented programming is the method of bundling related properties into individual objects.

OOP is that in which we structure a program so that the properties and behaviors are bundled into individual objects. An object could represent a person with properties like name, age, height, and behaviors such as walking, talking, etc.

The key takeaway is that objects are at the center of object-oriented programming in Python, not only representing the data, as in procedural programming, but in the overall structure of the program as well.

We use classes in python so that we make the code more manageable and maintainable.

Classes and instances

Classes are used to create user-defined data structures. Classes define functions called methods, which identify the behaviors and actions that an object created from the class can perform with its data. A class is a blueprint for how something should be defined. It doesn’t contain any data.

Now, if we define a class named man, and that contains the name and age of a person. That class specifies that the name and age are important for defining a man, but it doesn’t contain any name or age of a man.

An instance is an object that is built from a class and contains real data.

Defining a class

All class definitions start with the class keyword, which is followed by the name of the class and a colon. Any code that is indented below the class definition is considered part of the class’s body.

class Man:
pass

The pass keyword is often used as a placeholder indicating where the code will eventually go. It allows you to run this code without Python throwing an error.

The properties that all Man objects must be defined in a method called __init__(). Every time a new Man is created,__init__() sets the initial state of the object by assigning the values of the object’s properties. So, __init__() initializes each new instance of the class.

The first parameter of the __init__() should be a variable called self. When a new class instance is created, the instance is automatically passed to the self parameter in __init__() so that new attributes can be defined in the object.

class Man:
def __init__(self, name, age):
self.name = name
self.age = age

self.name = name: creates an attribute called name and assigns to it the value of the name parameter.

self.age = age: creates an attribute called age and assigns to it the value of the age parameter.

The attribute created is called instance attributes. These are specific to a particular instance of the class. All the Man objects have a name and age, but the values differ over the instance.

Class attributes are attributes that have the same value for all class instances. You can define a class attribute by assigning a value to a variable name outside of __init__().

class Man:
planet = 'earth'
def __init__(self, name, age):
self.name = name
self.age = age

Class attributes are defined directly beneath the first line of the class name. They must always be assigned an initial value. When an instance of the class is created, class attributes are automatically created and assigned to their initial values.

Use class attributes to define properties that should have the same value for every class instance. Use instance attributes for properties that vary from one instance to another.

Instance methods

Instance methods are the functions that are defined inside a class, and can only be called from an instance of the class.

class Man:
def __init__(self,name,age):
self.name = name
self.age = age
def things(self,ability):
return f"{self.name}\'s ability is {ability}"
def about(self):
return f"{self.name} and is {self.age}"
keshav = Man("keshav", 18)
print(keshav.things("sleeping"))
print(keshav.about())

From the above, if we print keshav we would get something like “<__main__.Man at 0x7fcb1ee57e10>”. Now, to tackle that we __str__()

Inherit from other classes

Inheritance is the process by which one class takes on the attributes and methods of another. Newly formed classes are called child classes, and the classes that child classes are derived from are called parent classes.

Child classes can override or extend the attributes and methods of parent classes. In other words, child classes inherit all of the parent’s attributes and methods but can also specify attributes and methods that are unique to themselves.

class Man:
def __init__(self,name,age):
self.name = name
self.age = age
def things(self,ability):
return f"{self.name}\'s ability is {ability}"
def about(self):
return f"{self.name} and is {self.age}"
sr = Man('SR Keshav', 18)
k = Man('Keshav', 18)
print(sr.things('sleep'),";",k.things('eat'))

To check whether a class is an instance or not we use a built-in function isinstance(). The isinstance() function returns True if the specified object is of the specified type, otherwise False.

class myObj:
name = "John"
y = myObj()
x = isinstance(y, myObj)

To override a method defined on the parent class, you define a method with the same name on the child class.

You made it till the end, thank you!

--

--