Skip to content

Latest commit

 

History

History
46 lines (41 loc) · 805 Bytes

File metadata and controls

46 lines (41 loc) · 805 Bytes

Dunder Methods, Dataclass, slots, MRO, eq edge case

Dunder Methods

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def __str__(self):
        return f"({self.x}, {self.y})"
    def __eq__(self, other):
        return self.x == other.x and self.y == other.y

Dataclass

from dataclasses import dataclass
@dataclass
class Item:
    name: str
    price: float

slots

class Slim:
    __slots__ = ['x', 'y']
    def __init__(self, x, y):
        self.x = x
        self.y = y

MRO

class A: pass
class B(A): pass
print(B.__mro__)

eq without hash (Edge Case)

class BadEq:
    def __eq__(self, other):
        return True
# hash(BadEq())  # TypeError: unhashable type