[Tutor] Why do I not get an error when I mistakenly type "humdrum.sigh_strenght" instead of the correct "humdrum.sigh_strength"?

Ben Finney ben+python at benfinney.id.au
Thu Jan 21 02:01:43 EST 2016


boB Stepp <robertvstepp at gmail.com> writes:

> My intent was to deliberately introduce an error into my class definition:
>
> >>> class Hmm(object):
>         def __init__(self, sigh_type, sigh_strength):
>             self.sigh_type = sigh_type
>             self.sigh_strength = sigh_strength
>         def snort(self):
>             if self.sigh_strength == 'low':
>                 print("snort")
>             elif self.sigh_strength == 'med':
>                 print("Snort!")
>             elif self.sigh_strenght == 'high':
>                 print("SNORT!!!")
>             else:
>                 print("Hmm...")
>         def sigh():
>             if self.sigh_type == 'quiet':
>                 print("pssssss")
>             elif self.sigh_type == 'annoying':
>                 print("Whoosh!")
>             elif self.sigh_type == 'loud':
>                 print("HEAVY SIGH!!!")
>             else:
>                 print("HMM!!!")
>
> I omitted "self" from the sigh() method to see what would happen

What would happen, when? At the time of class definition, there are no
errors that I can see.

Python doesn't run every possible branch of your code ahead of time,
just to find out what it will do. Most errors will be discovered only by
encountering the combination of inputs that will trigger them at run
time.

For this and other reasons, it is highly recommended to develop unit
tests with your code, to make explicit assertions about exactly what
will trigger each branch of the code.

This, in turn, will lead to making your code simpler and less-branching
:-)

-- 
 \     “Do unto others twenty-five percent better than you expect them |
  `\      to do unto you. (The twenty-five percent is [to correct] for |
_o__)                            error.)” —Linus Pauling's Golden Rule |
Ben Finney



More information about the Tutor mailing list