Mysterious symbols
Student Login

Mysterious symbols

A reader asked a great question. Great because it's a little complex.

Look at this class:

  1. class Polynomial:
  2. def __init__(self, *coeffs):
  3. self.coeffs = coeffs
  4. def __repr__(self):
  5. return 'Polynomial(*{!r})'.format(self.coeffs)
  6. def __add__(self, other):
  7. return Polynomial(*(x + y for x, y in zip(self.coeffs, other.coeffs)))
  8. def __len__(self):
  9. return len(self.coeffs)

The question is, basically, "What's going on with this class"?

More precisely:

What's interesting is that if your goal is to understand WHY this code is written this way, you almost have to answer all these questions together. Because the "why" is intertwined.

That's what real code is like.

Right? And toy code is NOT like.

I mean, toycode can demonstrate a single language feature. In isolation.

That's useful, sure.

But:

Realistic applications have a lot going on... moving parts that move together. Nothing in isolation.

That's why it's important to study such code. Make sure you do that, and learn from it.

Newsletter Bootcamp

Book Courses