A guide to knowing the Python OOP concepts better.

Object-oriented programming abbreviated as OOP allows programmers to create their own objects that have methods and attributes.

These methods act as functions that use information about the object as well as the object itself to return results or change the current object.

OOP allows users to create their own objects. It helps us to create code that is repeatable and organized.

Syntax

The basic way to define an object is by using the class keyword. The class keyword is used and the class name is written after that. The class name follows the rule of camel casing where every individual word is capitalized.

Next, we have written a function but actually, it is a method because it is inside a class call. The special method “init” is defined. This allows you to create the instance of the actual object. The arguments are a ‘self’ keyword and 2 parameters. ‘param1’ and ‘param2’ are the parameters that Python expects you to pass when we create an instance of this object.

When you pass in a parameter say ‘param2’, it is assigned to an attribute of the function. That way Python knows that when you refer to self.param2, you are referring to the attribute ‘param2’ that is connected to the actual instance of the class.

Then you can create other functions and pass ‘self’ keyword to let Python know that it is not just any function but a method that is connected to the class.

>>> class ClassName():
        def __init__(self, param1, param2):
            self.param1 = param1
            self.param2 = param2
       def some_method(self):
           #perform some action
           print(self.param1)

#methods #object-oriented #coding #oop #python

Object-Oriented Programming in Python
1.45 GEEK