Skip to content

Instantly share code, notes, and snippets.

@ItsDrike
ItsDrike / 1_basic_autoinit.py
Last active May 6, 2022 22:06
Python auto_init
"""
This attempts to abstarct away the standard way of using `__init__`,
the problem it tries to solve is the repetetiveness of using init purely
to store it's parameters into the instance under the exactly same name, i.e.:
class Stock:
def __init__(name, shares, price):
self.name = name
self.shares = shares
self.price = price
"""
@ItsDrike
ItsDrike / descriptors.py
Created January 3, 2021 00:42
Explanation of python's descriptors
class ClassicalItem:
"""
The classical solution, there's nothing special about it,
but if we came from old implementation without this protection,
we would loose backwards compatibility, because `amount` attribute
wouldn't be accessible anymore.
"""
def __init__(self, description, amount, price):
self.description = description
self.set_amount(amount)