Project 2 · Lecture 01 · 8 min
Project - Description
Starting from the Polygon and Polygons classes of Project 1, make every computed property lazy and turn the polygon sequence into a lazy iterable.
Where we start
In Project 1 you built two classes:
Polygon(n, R): a regular, strictly convex polygon withnedges and circumradiusR. It’s immutable (only read-only properties) and exposes calculated properties:interior_angle,side_length,apothem,areaandperimeter. Polygons compare equal when they have the same number of vertices and the same circumradius, and are ordered by number of vertices.Polygons(m, R): a sequence of all polygons with 3 up tomvertices and a common circumradiusR, with amax_efficiency_polygonproperty returning the polygon with the highest area-to-perimeter ratio.
Here’s the code we’ll use as the starting point (use your own version if you prefer):
import math
class Polygon:
def __init__(self, n, R):
if n < 3:
raise ValueError('Polygon must have at least 3 vertices.')
self._n = n
self._R = R
def __repr__(self):
return f'Polygon(n={self._n}, R={self._R})'
@property
def count_vertices(self):
return self._n
@property
def count_edges(self):
return self._n
@property
def circumradius(self):
return self._R
@property
def interior_angle(self):
return (self._n - 2) * 180 / self._n
@property
def side_length(self):
return 2 * self._R * math.sin(math.pi / self._n)
@property
def apothem(self):
return self._R * math.cos(math.pi / self._n)
@property
def area(self):
return self._n / 2 * self.side_length * self.apothem
@property
def perimeter(self):
return self._n * self.side_length
def __eq__(self, other):
if isinstance(other, self.__class__):
return (self.count_edges == other.count_edges
and self.circumradius == other.circumradius)
return NotImplemented
def __gt__(self, other):
if isinstance(other, self.__class__):
return self.count_vertices > other.count_vertices
return NotImplemented
class Polygons:
def __init__(self, m, R):
if m < 3:
raise ValueError('m must be greater than 3')
self._m = m
self._R = R
self._polygons = [Polygon(i, R) for i in range(3, m + 1)]
def __len__(self):
return self._m - 2
def __repr__(self):
return f'Polygons(m={self._m}, R={self._R})'
def __getitem__(self, s):
return self._polygons[s]
@property
def max_efficiency_polygon(self):
sorted_polygons = sorted(self._polygons,
key=lambda p: p.area / p.perimeter,
reverse=True)
return sorted_polygons[0]A quick look at what it does:
p = Polygon(6, 2)
print(p, p.side_length, round(p.area, 4), p.interior_angle)
polygons = Polygons(6, 1)
print(polygons, len(polygons), list(polygons))
print(polygons[2], polygons[-1], polygons.max_efficiency_polygon)What’s wasteful about it
Two things in this design do more work than necessary:
Polygonrecomputes everything on every access.areacallsside_lengthandapothem, which callmath.sinandmath.cos, every single time. But aPolygoncan’t change after it’s created, so each of those values only ever needs to be computed once.Polygonsbuilds every polygon up front.Polygons(1000, 1)immediately creates 998Polygonobjects and keeps them all in memory, even if you only look at the first one. The polygons are completely determined bymandR, so they can be created on demand.
Goal 1: lazy properties
Refactor Polygon so that every calculated property (interior_angle, side_length, apothem, area, perimeter) is lazy:
- nothing is computed when the polygon is created
- a value is computed the first time it’s requested, then cached
- later requests return the cached value without recomputing
Since Polygon is immutable, a cached value can never go stale, so you don’t need any invalidation logic. Everything else (validation, repr, equality and ordering) must keep working exactly as before.
Goal 2: a lazy iterable
Refactor Polygons from a sequence into an iterable:
- write an iterator class (call it
PolygonsIterator) that hands outPolygon(3, R),Polygon(4, R), …,Polygon(m, R)one at a time, creating each polygon only whennext()asks for it Polygons.__iter__must return a new iterator each time, so aPolygonsobject can be looped over as often as you like- don’t store the polygons in a list (or any other container);
Polygonsshould only remembermandR - keep
len(), therepr, them < 3check, and themax_efficiency_polygonproperty, which should also be lazy: computed once, on first access
Hints
- A lazy property needs a private “backing” attribute (for example
self._area), initialized toNonein__init__. The property computes and stores the value only if the backing attribute is stillNone. - The iterator needs to remember which polygon to produce next. A counter starting at 3 works well.
sorted()andmax()accept any iterable, including your newPolygonsobject.- Test laziness, not just results: count how often
math.sinis called, or how manyPolygonobjects are created.
Try it yourself
Before reading the solution walkthroughs, open this section’s Practice page. Each goal is an auto-graded exercise with starter code and tests that check both the results and the laziness of your implementation.
Key takeaways
- Goal 1: every calculated
Polygonproperty becomes lazy: computed on first access, cached afterwards. - Goal 2:
Polygonsbecomes an iterable whose iterator creates polygons on demand, with no stored list. max_efficiency_polygonmust be lazy too.- Immutability is what makes caching safe here: the inputs never change, so cached values never go stale.