[Tutor] How to type annotate complex dictionary containing lambdas?
DL Neil
PyTutor at danceswithmice.info
Sun Nov 17 17:04:33 EST 2019
On 18/11/19 8:12 AM, boB Stepp wrote:
> On Sun, Nov 17, 2019 at 2:52 AM Alan Gauld via Tutor <tutor at python.org> wrote:
>> On 17/11/2019 05:32, boB Stepp wrote:
>>> My best effort so far to type annotate the dictionary below is to use
>> To be honest I wouldn't even be trying I'd be more concerned with
>> getting rid of such a messy dict and replacing it with a class.
>>
>> Type hints are no substitute for clean code.
>
> Chastisement taken, accepted and fully merited!
+1
So, as you later allude, the *first* problem was the lack of tests...
> As I was staring at this program last night I was trying to recreate
> in memory its history of "development" (Ha!). Originally it was an
> extremely short program with no comments, no docstrings, no input
> validation. It was just a quick and dirty throw-away program to
...
> really was curious how such a beast would be properly type annotated.
...which led to the second problem, not so apparently recognised: that
whilst this situation is common, in the spirit of
learning-by-experimentation ("constructivism") and insatiable curiosity,
we end-up with fairly-messy code. During 'the grand experiment' it was
finally wrangled into some semblance of working code; and we moved-on.
However, in coming back to it, the realisation of today's superior
knowledge and capability should be turned-into a re-factored and
improved version of the existing code. Thereafter, today's experiment
will build upon a more stable foundation!
(else, some risk that today's learning-mistakes are actually
side-effects from time-before)
> One thing that trying to write tests has taught me: If it is
> difficult to write tests for whatever, then something probably needs
> to be refactored/done better.
+1
As you might guess, I did not write any
> tests for this program. And apparently type annotations teach a
> similar sort of wisdom: If it is hard to figure out how to annotate a
> given block of code, something really, really stinks!
+1
and yes, +1 on the class-upgrade (gives more leg-room on the plane!). We
keep the dictionary structure and add (more flexible) functionality.
[oops, in saying that, I fear @Alan will come-after me: So, in a bid to
prevent the tooth- and claw-marks: a class *first* gives function and
*then* offers us a dictionary/data-structure 'for free' (sound of
running footsteps to be heard, leaving stage right, rapidly)...]
--
Regards =dn
More information about the Tutor
mailing list