I come from static-type programming and I'm interested in understanding the rationale behind dynamic-type programming to check if dynamic-type languages can better fit my needs.
I've read about the theory behind duck programming. I've also read that unit testing (desirable and used in static-type programming) becomes a need in dynamic languages where compile-time checks are missing.
However, I'm still afraid to miss the big picture. In particular, how can you check for a mistake where a variable type is accidentally changed ?
Let's make a very simple example in Python:
#! /usr/bin/env python
userid = 3
defaultname = "foo"
username = raw_input("Enter your name: ")
if username == defaultname:
# Bug: here we meant userid...
username = 2
# Here username can be either an int or a string
# depending on the branch taken.
import re
match_string = re.compile("oo")
if (match_string.match(username)):
print "Match!"
Pylint, pychecker and pyflakes do not warn about this issue.
What is the Pythonic way of dealing with this kind of errors ?
Should the code be wrapped with a try/catch ?