Skip to content

Instantly share code, notes, and snippets.

@raeq
Last active May 2, 2021 12:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raeq/464654399a932d2f95adc0da99f4e4b3 to your computer and use it in GitHub Desktop.
Save raeq/464654399a932d2f95adc0da99f4e4b3 to your computer and use it in GitHub Desktop.
Worked example of overloading
import math
from functools import singledispatch
class Square:
length: int
def __init__(self, length=0):
self.length = length
class Circle:
radius: int
def __init__(self, radius=0):
self.radius = radius
from functools import singledispatch
@singledispatch
def area(any_object):
raise NotImplementedError
@area.register(Circle)
def _(any_object):
return math.pi * (math.pow(any_object.radius, 2))
@area.register(Square)
def _(any_object):
return (math.pow(any_object.length, 2))
my_circle = Circle(6)
my_square = Square(12)
print(area(my_circle))
print(area(my_square))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment