Skip to content

Instantly share code, notes, and snippets.

@aynm142
Created February 5, 2017 20:41
Show Gist options
  • Save aynm142/8e73ab49651e005c4ab49a68da92848f to your computer and use it in GitHub Desktop.
Save aynm142/8e73ab49651e005c4ab49a68da92848f to your computer and use it in GitHub Desktop.
Example
class Rectangle:
def __init__(self, side_a, side_b):
self.side_a = side_a
self.side_b = side_b
def __repr__(self):
return "Rectangle(%.1f, %.1f)" % (self.side_a, self.side_b)
class Circle:
def __init__(self, radius):
self.radius = radius
def __repr__(self):
return "Circle(%.1f)" % self.radius
@classmethod
def from_rectangle(cls, rectangle):
radius = (rectangle.side_a ** 2 + rectangle.side_b ** 2) ** 0.5 / 2
return cls(radius)
def main():
rectangle = Rectangle(3, 4)
print(rectangle)
first_circle = Circle(1)
print(first_circle)
second_circle = Circle.from_rectangle(rectangle)
print(second_circle)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment