>>>> humdrum.sigh_strenght = 'high'
>>>> humdrum.snort()
> Snort!
>
> At this point I wondered why my output was not "SNORT!!!".  Then I
> noticed my typo.  But now I wonder why I did not get an error from
> this typo?


Hi boB,


Just to be explicit: you are pointing out that:

     humdrum.sigh_strenght = 'high'

was a typo, and it would have been nice if it could be caught as an error.

If that's the case, then yes, I agree.  Unfortunately, this isn't a
runtime error because Python allows us to add arbitrary attributes to
objects: it's a deliberate design feature.  One justification for this
appears to be similar to that of dynamic typing: if we have good unit
tests, we should be able to catch these problems with our unit tests,
so why add a restriction?

(As a personal note: I'm not entirely convinced of the merit of this
particular freedom.  Misspelling is a really easy mistake to make, and
I want the language to catch my simple mistakes, if it's not too
onerous to do so.)


That being said, there are tools available that should be able to
catch many of these problems before runtime.

    http://www.pylint.org/
    http://pychecker.sourceforge.net/

These should raise warnings if you run it on your program.  You might
want to take a look at these!


Furthermore, there actually *is* a way to turn it into a runtime
error, though this is probably not beginner material.  If you're
interested, see material on "__slots__".
https://docs.python.org/3/reference/datamodel.html#slots.  I don't
think it's intended to be used to catch typos though: its main purpose
is to reduce memory usage, and it's use to catch misspellings is
incidental.


Hope this helps!
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to