Posts

Showing posts with the label Python Object Oriented Programming

Python Object Oriented Programming - Polymorphism

Image
Polymorphism means many forms. It is one of the most crucial concepts in programming. It refers to the use of a single entity like method or operator to represent different types different use cases. Function polymorphism Python has several functions that accept different types of arguments. One such function is len() which accepts different types of data. Here is an example of polymorphism using len(): str = 'Knowledge Factory'       #String my_list = [ 1 , 33.3 , 'Carl' ]     #List elem = ( 4 , 'python' , 2 + 3 j )       #Tuple a = { 5 , 2 , 3 , 1 , 4 }                 #Set d = { 123 : 'python' , '234' : 'go' }   #Dictionary print(len(str))     #Length of String print(len(my_list)) #Length of List print(len(elem))     #Length of Tuple print(len(a))       #Length of Set print(len(d))       #Length of Dictionary Console output: 17 3 3 5 2 Here, you can see that the len() function can be used with different types of data, such as strings,

Python Object Oriented Programming - Inheritance

Image
Inheritance is a fundamental concept in object-oriented programming. It allows you to reuse properties and methods of an existing class to create a new one.  Python makes inheritance quite simple and intuitive.  In this post, we will discuss the basics of inheritance with Python examples. Basic Inheritance Concepts   Inheritance is the ability of a class (child) to inherit attributes and methods from another class (parent) to avoid code duplication and simplify changes and updates. Creating a Parent Class First, let's design a base class called Animal that will have common attributes and methods for all animals. class Animal:     def __init__(self, name, age):         self .name = name         self .age = age       def sound(self):         print( f " { self .name} makes a sound." ) Creating a Child Class Now, let’s make a class called "Dog" that inherits the class Animal’s properties and methods. We do this by putting the parent class name in parentheses aft

Python Object Oriented Programming - Classes and Objects

Image
There are numerous built-in types in Python, such int, str, and others, that we can utilize in our programs. However, Python also lets you use classes to define your own kinds.  A class is an object-creation code template. Objects have behaviors and member variables attached to them. The keyword 'class' in Python creates a class. Using the class constructor, an object is made. Everybody knows something about a person with a name, age, and a few more details. A person is capable of walking, running, thinking, and other activities. In other words, we can refer to this representation—which consists of a collection of traits and behaviors—as a class. The particular way that this pattern is embodied varies; for instance, some persons only have one name, while others have multiple names. Additionally, a real person will represent an object of this class. The class is defined using the keyword 'class': class class_name:     class_attributes     class_methods In a class, attri