Logo

dev-resources.site

for different kinds of informations.

Python's Magic Methods

Published at
12/29/2024
Categories
python
dundermethod
oop
cleancode
Author
jaiminbariya
Categories
4 categories in total
python
open
dundermethod
open
oop
open
cleancode
open
Author
12 person written this
jaiminbariya
open
Python's Magic Methods

1 -> __new__(cls) Method

The __new__ method is called when a new object is created in Python. It's responsible for creating and returning the new instance of the class. This method is usually used when you want to customize object creation, such as for singleton patterns, caching, or managing memory.

When is __new__ Called?

The __new__ method is called before __init__ and is used to create the new object. Here's the typical order of events when you create a new object:

  1. __new__: Creates the object (memory allocation).
  2. __init__: Initializes the object (setting up attributes).

Use Cases for __new__:

  1. Singleton Pattern: The Singleton pattern ensures that only one instance of a class exists. In this case, __new__ checks if an instance already exists and reuses it, instead of creating a new one.
   class Singleton:
       _instance = None

       def __new__(cls):
           if cls._instance is None:
               cls._instance = super(Singleton, cls).__new__(cls)
           return cls._instance

   s1 = Singleton()
   s2 = Singleton()
   print(s1 is s2)  # True, both are the same instance
Enter fullscreen mode Exit fullscreen mode
  1. Caching Objects: If you want to cache objects based on certain conditions, you can use __new__ to check if an object already exists (e.g., in a dictionary) before creating a new one. This can help optimize memory usage.
   class CachedObject:
       _cache = {}

       def __new__(cls, value):
           if value in cls._cache:
               return cls._cache[value]
           obj = super().__new__(cls)
           cls._cache[value] = obj
           return obj

   obj1 = CachedObject("hello")
   obj2 = CachedObject("hello")
   print(obj1 is obj2)  # True, the same object is reused
Enter fullscreen mode Exit fullscreen mode
  1. Memory Management:
    If you want to control the memory allocation of objects (e.g., to optimize memory usage or manage large objects), __new__ can be used to customize how objects are created.

  2. Immutable Objects:
    __new__ is often used with immutable objects like tuples and strings. For instance, when you want to create a custom immutable object, you would override __new__ to ensure it’s properly created.

   class MyTuple(tuple):
       def __new__(cls, *args):
           return super().__new__(cls, args)

   t = MyTuple(1, 2, 3)
   print(t)  # (1, 2, 3)
Enter fullscreen mode Exit fullscreen mode

In Summary:

  • __new__ is called when an object is being created and is responsible for returning the instance of the class.
  • It is useful for optimizing object creation, implementing patterns like Singleton, managing object caching, or even customizing the memory allocation process.

cleancode Article's
30 articles in total
Favicon
STOP Writing Dirty Code: Fix The Data Class Code Smell Now!
Favicon
Абстракции vs. привязка к технологии
Favicon
An Initiation to Domain-Driven Design
Favicon
7 Essential Design Patterns for JavaScript Developers: Boost Your Coding Mastery
Favicon
Orden en el Código .NET
Favicon
Movie X: A Developer’s Dive Into Flutter Project Organization
Favicon
3 Code Comment Mistakes You're Making Right Now
Favicon
From Chaos to Control
Favicon
Clean code
Favicon
Want to Learn Docker in Advance Way?
Favicon
3 very simple React patterns to immediately improve your codebase 🪄
Favicon
Refactoring 021 - Remove Dead Code
Favicon
Code Commenting Ethics: When Over-Documentation Hurts Development
Favicon
Clean Code: Managing Side Effects with Functional Programming
Favicon
Why Use Getters and Setters?!
Favicon
Clojure Is Awesome!!! [PART 4]
Favicon
Clojure Is Awesome!!! [PART 3]
Favicon
Python's Magic Methods
Favicon
Clojure Is Awesome!!! [PART 2]
Favicon
Why should I care about Quality? I'm a developer!
Favicon
Mastering Laravel Blade: @stack, @push, and @endpush
Favicon
How to write a good Clean code? - Tips for Developers with Examples
Favicon
Union and Intersection Types in TypeScript
Favicon
Arquitetura Viva: Moldando Sistemas para Mudanças
Favicon
Dependency Injection in ASP.NET Core with Extension Classes: A Comprehensive Guide
Favicon
Rails transactional callbacks beyond models
Favicon
Python Best Practices: Writing Clean and Maintainable Code
Favicon
Excited to Be Part of This Community! 🚀
Favicon
Single Responsibility Principle in Javascript
Favicon
Build Express APIs Faster than AI

Featured ones: