The very first thing one must learn and believe without a shred of doubt is that In python, everything is an object. Python offers a host of magic methods (alternatively called dunder methods because of the “double underscores”) to make custom objects more intuitive, easy to use and pythonic. Let’s start with a very basic scenario:
The very first thing one must learn and believe without a shred of doubt is that In python, everything is an object.
Python offers a host of
magic methods (alternatively called
dunder methods because of the “double underscores_”) to make custom objects more intuitive, easy to use and **_pythonic. **Let’s start with a very basic scenario:
string_sum_output = 'hello' + ' world' + '!' integer_sum_output = 10 + 20 print(string_sum_output) print(integer_sum_output) o/p1:>> hello world! o/p2:>> 30
At first glance
o/p2 seems super normal. But let me ask you this,
how does python decide when to do arithmetic sum and when to concatenate values**if ‘+’ operator is used**?
Most obvious answer will be based on data-types, which is not incorrect, but doesn’t reveal the real process of deciding though. That’s where
magic methods come into picture. Certain behaviours of an objects can be controled and dictated by using these, so called, magic methods.
So in this article, we will see _**_how to add certain behavioural traits to our custom classes**, so that objects of those classes will have some special functionalities.
Following is our simple
A Basic Student Class
A very basic class, which takes three parameters to define a student —
There are numerous magic methods available in python, we will see some basic and most widely used of those, to implement following scenarios:
**_length_**_ function on one object?_
Learn Python OOPS. We'll understand the different object-oriented programming concepts and how Python implements them by creating classes and objects. You will get an idea about inheritance, encapsulation, and polymorphism and perform a hands-on demo on these concepts.
OOP in Python will help you understand how to write various basic programs in python and real-time uses of some of the Python libraries as well.
Inheritance is a key concept in Object-Oriented Programming. It enables us to create a new class from an existing class. The new class is a specialized version of the existing class and it inherits all the non-private variables and methods of the existing class. When should we use inheritance? The answer is that whenever we come across an IS A relationship between objects.
In this tutorial, you’re going to learn a variety of Python tricks that you can use to write your Python code in a more readable and efficient way like a pro.
Today you're going to learn how to use Python programming in a way that can ultimately save a lot of space on your drive by removing all the duplicates. We gonna use Python OS remove( ) method to remove the duplicates on our drive. Well, that's simple you just call remove ( ) with a parameter of the name of the file you wanna remove done.