Skip to content

Instantly share code, notes, and snippets.

@goutomroy
Created May 26, 2019 12:35
Show Gist options
  • Save goutomroy/7388791d702ca4e013652e9edfbf767e to your computer and use it in GitHub Desktop.
Save goutomroy/7388791d702ca4e013652e9edfbf767e to your computer and use it in GitHub Desktop.
Uses example of commonly used python decorators.
class Circle:
def __init__(self, radius):
self._radius = radius
@property
def radius(self):
"""Get value of radius"""
return self._radius
@radius.setter
def radius(self, value):
"""Set radius, raise error if negative"""
if value >= 0:
self._radius = value
else:
raise ValueError("Radius must be positive")
@property
def area(self):
"""Calculate area inside circle"""
return self.pi() * self.radius**2
def cylinder_volume(self, height):
"""Calculate volume of cylinder with circle as base"""
return self.area * height
@classmethod
def unit_circle(cls):
"""Factory method creating a circle with radius 1"""
return cls(1)
@staticmethod
def pi():
"""Value of π, could use math.pi instead though"""
return 3.1415926535
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment