Consider this example of a function (and its use) that receives one parameter which is only meaningful as int:
def fun(index): index = int(index) # do something with index def main(): print fun(1e6) I find that I keep repeating this pattern on several functions that deal with integers since it ensures that I really receive some integer number (note that 1e6 is a floating point instance).
This is a bit cumbersome and if forgotten once it could produce problems (there is code that reacts differently to floating-point/integer/string/... values). So could this be somehow encoded in the function signature?
E.g. something along the lines of this pseudo-code:
def fun(int index): # do something with index
0and I had some loops overshoot. (But I'm right in the middle of learning Python, coming from C++ and Haskell --- so I'm not surprised that I have trouble accepting duck-typing.)indexfor a loop, use it asrange(index). That will raise an exception if you feed it an object that cannot be represented as an integer.