Magic Methods

str and repr

  1. # string representation
  2. class Point:
  3. def __init__(self, x, y):
  4. self.x = x
  5. self.y = y
  6. def __str__(self):
  7. return f"Point({self.x}, {self.y})"
  8. str(Point(3, 4))
  9. #ans: "Point(3, 4)"
  10. print(Point(3, 4))
  11. #ans: Point(3, 4)

len

  1. # len() support
  2. class MyList:
  3. def __init__(self, items):
  4. self.items = items
  5. def __len__(self):
  6. return len(self.items)
  7. len(MyList([1, 2, 3]))
  8. #ans: 3

getitem and setitem

  1. # indexing support
  2. class MyList:
  3. def __init__(self):
  4. self.items = []
  5. def __getitem__(self, index):
  6. return self.items[index]
  7. def __setitem__(self, index, value):
  8. self.items[index] = value
  9. ml = MyList()
  10. ml.items = [1, 2, 3]
  11. ml[0]
  12. #ans: 1

add and sub

  1. # operator overloading
  2. class Vector:
  3. def __init__(self, x):
  4. self.x = x
  5. def __add__(self, other):
  6. return Vector(self.x + other.x)
  7. Vector(5) + Vector(3)
  8. #ans: Vector(8)

eq and lt

  1. # comparison operators
  2. class Point:
  3. def __init__(self, x):
  4. self.x = x
  5. def __eq__(self, other):
  6. return self.x == other.x
  7. def __lt__(self, other):
  8. return self.x < other.x
  9. Point(5) == Point(5)
  10. #ans: True

Exercises - Part 1

  1. # __str__ vs __repr__?
  2. class MyClass:
  3. def __str__(self):
  4. return "str"
  5. def __repr__(self):
  6. return "repr"
  7. str(MyClass())
  8. #ans: "str"
  9. repr(MyClass())
  10. #ans: "repr"

Exercises - Part 2

  1. # __len__ return type?
  2. class MyClass:
  3. def __len__(self):
  4. return 5.5
  5. len(MyClass())
  6. #ans: TypeError (must return int)

Exercises - Part 3

  1. # __getitem__ for iteration?
  2. class MyClass:
  3. def __getitem__(self, i):
  4. if i >= 3:
  5. raise IndexError
  6. return i
  7. for x in MyClass():
  8. print(x)
  9. #ans: 0, 1, 2

Exercises - Part 4

  1. # __add__ with int?
  2. class MyClass:
  3. def __init__(self, x):
  4. self.x = x
  5. def __add__(self, other):
  6. return MyClass(self.x + other)
  7. MyClass(5) + 3
  8. #ans: MyClass(8)

Exercises - Part 5

  1. # __contains__?
  2. class MySet:
  3. def __init__(self, items):
  4. self.items = set(items)
  5. def __contains__(self, item):
  6. return item in self.items
  7. 2 in MySet([1, 2, 3])
  8. #ans: True

Exercises - Part 6

  1. # __call__?
  2. class Multiplier:
  3. def __init__(self, n):
  4. self.n = n
  5. def __call__(self, x):
  6. return x * self.n
  7. double = Multiplier(2)
  8. double(5)
  9. #ans: 10

Exercises - Part 7

  1. # __bool__?
  2. class MyClass:
  3. def __bool__(self):
  4. return False
  5. bool(MyClass())
  6. #ans: False

Exercises - Part 8

  1. # __iter__?
  2. class MyRange:
  3. def __init__(self, n):
  4. self.n = n
  5. def __iter__(self):
  6. return iter(range(self.n))
  7. list(MyRange(3))
  8. #ans: [0, 1, 2]

Exercises - Part 9

  1. # __enter__ and __exit__?
  2. class MyContext:
  3. def __enter__(self):
  4. return self
  5. def __exit__(self, *args):
  6. pass
  7. with MyContext() as ctx:
  8. pass

Exercises - Part 10

  1. # __del__?
  2. class MyClass:
  3. def __del__(self):
  4. print("Deleted")
  5. obj = MyClass()
  6. del obj
  7. #ans: "Deleted"

Google tag (gtag.js)