Advance Python Topics - 2

Advance Python Topics - 2

Again, before we proceed, everything in Python is an object.

1) Operator Overloading:

It is a sort of polymorphism in which we design an operator's behavior so that it can work with several user-defined data types depending on the context. The '+' operator, for example, adds two numbers or concatenates two lists or strings. But what if we want to add two objects of a custom class using the '+' operator? If we don't overload the operator within the class, it will throw an error.

In Python, every operator has a magic method( methods that begin and end with a double underscore.) associated with them that is invoked whenever we use that operator. Below is an example to add two complex numbers.

2) Exception Handling:

Python errors are classified into two categories. First, there are syntax errors, which arise when the program's syntax is incorrect. It immediately terminates the program. Second, an exception happens as a result of internal events or user-defined behavior, such as inputting an incorrect value into the program, which disrupts the usual flow of the program. Exceptions can and should be handled to ensure that the program runs smoothly.

Python includes various exceptions such as TypeError, NameError, IndexError, ZeroDivisionError, and so on. All of these exceptions are descendants of the Exception class. Exceptions are handled as described in the examples by using the try(some statements), except(executes if an error occurs in try), else(executes when no error occurs), and finally(always executes) blocks.

Also, we can throw an exception using the 'raise' keyword.

3) Context Manager:

Resources like files, database connections, etc. are limited and expensive. They should be managed and released appropriately once they are utilized. In Python, context manager can be implemented using the 'with' keyword. The syntax is as follows.

with context as ctx:

#some statements/operations

# context is automatically closed.

We can implement the context manager as a class by overriding the enter() and exit() methods.

4) Decorators:

In Python, functions are first-class objects i.e. they can be assigned to a variable, returned from another function, and passed as an argument to a function. Decorators accept a function as an argument, add some extra functionality to it, or modify the original behavior and return the modified function.

We can apply more than one decorator to a function. This process is known as decorator chaining.

@decorator_1

@decorator_2

@decorator_3

def my_simple_fun():

print("Hello")

Here, decorator 3 will be applied first, then 2, and then 1.

5) @staticmethod and @classmethod:

@staticmethod is a class level method i.e. it belongs to the class rather than the objects of the class. They cannot access or modify the state of an object or the class. They can be called using the class or their objects. We use static methods because for some methods, it makes logical sense for them to be present in that class as a utility method.

@classmethod also belongs to the class. They accept 'cls' as a default parameter. They can access and modify the state of the class. They are mostly used as a factory method design pattern.

To view or add a comment, sign in

Others also viewed

Explore content categories