OOP

Property

TL;DR Use @property decorator for getters and setter in OOP in a more pythonic way. Python programming provides us with a built-in @property decorator which makes usage of getter and setters much easier in Object-Oriented Programming.

2023-02-12

Instance, Class, and Static Methods

TL:DR Instance methods need a class instance and can access the instance through self. Class methods don’t need a class instance. They can’t accessthe instance (self) but they have access to the class itself via cls.

2022-12-16

Class vs Instance Variable

TL;DR Class variables are for data shared by all instances of a class. They belong to a class, not a specific instance and are shared among all instances of a class.

2022-12-16

Abstract Base Class (ABC)

TL;DR Abstract Base Classes (ABCs) ensure that derived classes implement particular methods from the base class at instantiation time Using Python’sabc module help avoid bugs and make class hierarchies easier to maintain.

2022-12-11

Object Cloning

TL;DR Making a shallow copy of an object won’t clone child objects. → The copy is not fully independent of the original. A deep copy of an object will recursively clone child objects.

2022-12-11

Define Your Own Exception Classes

TL;DR Defining your own exception types will state your code’s intent more clearly and make it easier to debug. Derive your custom exceptions from Python’s built-in Exception class or from more specific exception classes like ValueError or KeyError.

2022-12-04

String Conversion

TL;DR Control to-string conversion in your own classes using the __str__ and __repr__ “dunder” methods The result of __str__ should be readable. The result of __repr__ should be unambiguous. Always add a __repr__ to your classes.

2022-12-04

Object Comparison: == vs is

The == operator compares by checking for equality (the objects referred to by the variables are equal, i.e. have the same contents) The is operator comparies identities (whether two variables are in fact pointing to the same identical object) Example

2022-12-04

Operator Overloading

In Python, methods with __ (double underscore) prefix and suffix (e.g., __init__()) are special methods.They are called magic methods or dunder methods (dunder for “double underscore”). They can help override functionality for built-in functions for custom classes.

2022-05-04

OOP Basics

The concept of OOP in Python focuses on creating reusable code. This concept is also known as DRY (Don’t Repeat Yourself). Class A class is a blueprint for the object.

2022-05-04