Skip to Content
0 likes
Writing a Custom Iterator Class in Python With __iter__ and __next__ Level: Advanced

A for loop calls iter() once and next() until StopIteration. Write __iter__ and __next__ yourself, avoid the iterator that only works once, and know when a generator in __iter__ does the same job in fewer lines.

0 likes
Python dataclasses: A Class Without Writing __init__ by Hand Level: Intermediate

If a class mostly holds data, @dataclass writes __init__, a readable printout and equality for you. Use field(default_factory=list) for lists, __post_init__ for checks and frozen=True for values that must not change.

0 likes
Writing Your Own Python Context Manager, With a Class or contextlib Level: Advanced

Write a context manager when something must be undone no matter how the block ends. Build one as a class and with @contextmanager, keep yield inside try/finally, and don't swallow errors by accident.

0 likes
JavaScript Classes With Getters and Setters Level: Intermediate

Getters compute a value when you read it, so totals never go stale. Setters check a value when you assign it, so a negative price throws instead of saving. With private fields, nobody can go around them.

0 likes
Custom Error Classes You Can Actually Catch Level: Intermediate

Stop checking error messages with includes(). Extend Error, set name, add the fields the handler needs, catch with instanceof, and wrap low-level errors with cause so the real reason isn't lost.

0 likes
Fix a Bound Event Listener That Will Not Detach Level: Advanced

Trace a wrong callback receiver, bind one stable listener, and verify start, stop and restart without duplicate or stale subscriptions.

0 likes
private Fields vs #private: Which One Fits Level: Intermediate

Run the same class with the private keyword and with a hash field, and compare the output, the errors and what outside code can still reach. After this, you can pick one and say why.