DEV Community

Cover image for Object Oriented Programming
ASHLEY KALONDU
ASHLEY KALONDU

Posted on

Object Oriented Programming

I recently was working on creating codes to build a kiosk inventory and actions. I was creating dictionaries of the inventory(item, price, quantity, weight) and functions to cover the respective actions (restocking, updating prices, checking stock ) and it was manageable because my kiosk had only 3 items bread, milk and sugar.

When it comes bigger complicated stocks and many functions to be performed this would have been tedious, and this is where Object oriented Programming comes in

OOP

is a way of organizing programs by creating objects that contain their own data and the actions that can be performed on that data

Class

is a blue print that describes the attributes and methods that can apply on its objects

E.g age = 25, here age is a variable that refers to an object 25 which is in the class integers and it does not allow us to perform the .upper() method to it.
When you try print(age.upper()) you will break your code because under class integers we can't perform some methods e.g .Upper()

So the blueprint integers has set methods and attributes for its objects.
We can apply this method to a string because its class allows us to perform this method , but other methods that apply in class integers won't apply in class strings

Object

is an instance created under a class and holds some data (attributes)and can use the methods defined by its class.

How to write a class

Write the keyword class followed by the name:
Class name should be written using Camel case type of naming.

Methods

Its a function inside a class. def

init

This has double underscores on both sides referred to as dunder , and it allows us to give each object its information
This is the setup method to run when a new object is created.

Attributes

There are 2 types of attributes:

1. Instance Attributes

is the information that describes the object and are described using the word .self
Its main purpose is to give description to the object

2. Class Attributes

is a description in Class that covers all objects. All objects can be described using this attribute because its woven in the blue print.

Self

Because objects have similar attributes e.g in my example there will be price for milk , price for sugar so to ensure the right price falls under the right object we use self.
Self is not an attribute it just refers to the object and its always written first.

Top comments (0)