Python OOP Concepts

Python OOP Concepts

Learn Object Oriented Programming with examples

Β·

4 min read

Hey welcome to my blogs

To give you a summary of myself and why I am starting to blogging

I am Rohit Pimpale 2nd year in IT engineering and I am enthusiastic about ML. So I decided to blog because I wanted to share knowledge and document my ML engineering journey. I will be giving you information to my fullest knowledge and providing you with useful and practical knowledge. That's it about myself enjoy reading ;)

Object

In Python, everything is an object. The actual meaning of the object is "An object is an instance of a class." One way to understand the concept of objects and classes in object-oriented programming is to use a simple analogy. You can imagine an object as a single piece of furniture, like a chair, and a class as a whole set of furniture that includes chairs, tables, dishes, decorations, and more.

Class

User-defined objects are created using the class keyword. A class is like a blueprint that defines the properties and behavior of an object. In the furniture set analogy, the class would define the properties and methods that are common to all the pieces of furniture in the set, such as their overall design, material, and functionality.

# Create a new object type called furniture
class furniture:
    print ("The chair is part of furniture")
# Instance of furniture
furniture()

Attributes and Method

Attributes are essentially variables that belong to an object. Attributes define the state of an object, meaning they represent the values that the object holds at any given time

the syntax for creating attributes:

self.attribute = something
#special method
def __init__(self, name):
    self.name = name

Methods, on the other hand, are functions that belong to an object. They are used to perform operations with the attributes of our objects. You can basically think of methods as functions acting on an Object that takes the Object itself into account through its self argument.

class Circle:
    pi = 3.14

    # Circle gets instantiated with a radius (default is 1)
    def __init__(self, radius=1):
        self.radius = radius 
        self.area = radius * radius * Circle.pi
    # Method for getting Circumference
    def getCircumference(self):
        return self.radius * self.pi * 2
c = Circle()

print('Radius is: ',c.radius)
print('Area is: ',c.area)
print('Circumference is: ',c.getCircumference())
Summary
attributes define the state of an object, while methods define its behavior.

Inheritance

In Python, you can create a new class that inherits from an existing class by specifying the existing class in parentheses after the new class name. Important benefits of inheritance are code reuse and reduction of complexity of a program.

class animal:
    def __init__(self,name,sound):
        self.name = name
        self.sound = sounnd
        print("Animal created")
    def bark(self):
        print (f"{sound}")
class dog(animal):
    def __init__(self,name):
        #super is key word for inheritance
        super().__init__(name,"woof")
dog = Dog("Bond")
dog.bark() #animal class inheritanted by dog class and print function bark

Polymorphism

In Python, polymorphism refers to the way in which different object classes can share the same method name, and those methods can be called from the same place even though a variety of different objects might be passed in. The best way to explain this is by example:

class Animal:
    def __init__(self, name):    # Constructor of the class
        self.name = name
    def speak(self):         # Abstract method, defined by convention only
        raise NotImplementedError("Subclass must implement abstract method")
class Dog(Animal):
    def speak(self):
        return self.name+' says Woof!'   
class Cat(Animal):
    def speak(self):
        return self.name+' says Meow!'   
fido = Dog('Bond')
isis = Cat('Nami')
print(fido.speak())
print(isis.speak())
Real life examples of polymorphism include:
opening different file types - different tools are needed to display Word, pdf and Excel files

Abstraction

Abstraction is the process of simplifying complex systems by breaking them down into smaller, more manageable parts. In Python, this is typically achieved through the use of abstract classes and interfaces, which define a set of methods that must be implemented by any class that inherits from them.

Encapsulation

Encapsulation is the process of hiding the internal workings of an object from the outside world. In Python, this is typically achieved by making certain properties and methods private, so that they can only be accessed from within the class.

Conclusion

In conclusion, understanding the key concepts of object-oriented programming in Python is essential for anyone interested in developing software using this powerful and popular programming language. In this blog post, we covered some of the foundational concepts of OOP in Python, including objects, classes, attributes, methods, inheritance, and polymorphism. By mastering these concepts, you'll be well on your way to building robust, flexible, and reusable software applications using Python.

If you're interested in learning more about machine learning and data science, be sure to subscribe to this blog for weekly updates on new topics and insights. We'll be covering everything from basic concepts and techniques to advanced topics and cutting-edge research. And if you found this post helpful, please don't hesitate to like and share it with your friends and colleagues who may also benefit from this information. Thanks for reading!

Β