__init__ initializes an already-created instance. __new__ actually creates the instance. It’s a static method (though not decorated as such) that receives the class and returns a new instance.
Use case examples:
class Singleton:
_instance = None
def __new__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
By default, Python stores instance attributes in a __dict__ — flexible but memory-hungry. __slots__ tells Python to use a fixed array.
class Point:
__slots__ = ('x', 'y')
def __init__(self, x, y):
self.x = x
self.y = y
Effects:
Downsides:
👉 Use slots only when profiling shows object overhead is a bottleneck (e.g., in large data models or game entities).
In Python, everything is an object. Every integer, function, class, and module is an object allocated on the heap. Each object has:
class MyClass:
pass
obj = MyClass()
print(type(obj)) # <class 'main.MyClass'>
print(type(MyClass)) # <class 'type'> – yes, classes are objects too!
Understanding this is crucial: classes are runtime objects, not just compile-time blueprints.
Python is a multi-paradigm language. You can write functional, procedural, or OOP code. However, as projects scale beyond 1,000 lines, OOP becomes indispensable for:
High-quality OOP isn’t about using class everywhere. It’s about designing systems where objects have single responsibilities, well-defined boundaries, and predictable behavior.
Python 3 Deep Dive Part 4 Oop High Quality · Must Read
__init__ initializes an already-created instance. __new__ actually creates the instance. It’s a static method (though not decorated as such) that receives the class and returns a new instance.
Use case examples:
class Singleton:
_instance = None
def __new__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
By default, Python stores instance attributes in a __dict__ — flexible but memory-hungry. __slots__ tells Python to use a fixed array.
class Point:
__slots__ = ('x', 'y')
def __init__(self, x, y):
self.x = x
self.y = y
Effects:
Downsides:
👉 Use slots only when profiling shows object overhead is a bottleneck (e.g., in large data models or game entities).
In Python, everything is an object. Every integer, function, class, and module is an object allocated on the heap. Each object has: python 3 deep dive part 4 oop high quality
class MyClass:
pass
obj = MyClass()
print(type(obj)) # <class 'main.MyClass'>
print(type(MyClass)) # <class 'type'> – yes, classes are objects too!
Understanding this is crucial: classes are runtime objects, not just compile-time blueprints. __init__ initializes an already-created instance
Python is a multi-paradigm language. You can write functional, procedural, or OOP code. However, as projects scale beyond 1,000 lines, OOP becomes indispensable for:
High-quality OOP isn’t about using class everywhere. It’s about designing systems where objects have single responsibilities, well-defined boundaries, and predictable behavior.