Skip to content

Instantly share code, notes, and snippets.

@terror
Last active June 8, 2021 17:25
Show Gist options
  • Save terror/f253eff77d696f06dbb3379586f6b2f8 to your computer and use it in GitHub Desktop.
Save terror/f253eff77d696f06dbb3379586f6b2f8 to your computer and use it in GitHub Desktop.
Python metaprogramming example
class Meta(type):
def __new__(cls, what, bases=None, dict=None):
if 'area' not in dict:
raise Exception("Area not found!")
return type.__new__(cls, what, bases, dict)
class Shape(metaclass=Meta):
def __init__(self, l, w):
self.l = l
self.w = w
def area(self):
return self.l * self.w
class Square(Shape):
def __str__(self):
return f"[Square] Width: {self.w}, Length: {self.l} Area: {self.area()}"
def area(self):
return super().area()
class Triangle(Shape):
def __str__(self):
return f"[Triangle]: Width: {self.w}, Length: {self.l}, Area: {self.area()}"
def area(self):
return (self.l * self.w) // 2
def main():
print(Square(2, 2))
print(Triangle(2, 3))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment