I've seen a number of questions like following one: What's the canonical way to check for type in Python?
And there's always someone answering something like: "The pythonic way of checking types is not checking them. and here goes another passage about duck typing."
First of all, I do understand pros of duck typing, and I do use it quite a lot. But is it really worth not to check types?
Suppose I have following code:
class DuckA:
def quack():
print("QuackA")
class DuckB:
def quack():
print("QuackB")
def duck_creator():
return DuckA()
def duck_client(duck):
duck.quack()
if __name__ is "__main__":
duck_client(DuckA()) #ok
duck_client(DuckB()) #ok
duck_client(duck_creator()) #ok
#totally fine untill you actually call it,
#which might be quite tricky to check in
#relatively big project
duck_client(duck_creator)
#one more typo, which is pretty hard to spot
#from first sight
duck_client(DuckB)
Yes, I do realize that we're all engineers and thus, we're supported to write adequate constructions, but what about all kinds of typos?
I'm a beginner in python, and I came from c/c++ crowd. Basically, all those answers involving duck typing sound to me somewhat like "if you don't want to spend hours in debugger, you just need to write code without errors".
So, python gurus, is there are any valid/pythonic/accepted techniques to overcome things like that?
I've seen all kinds of type checkers, which are good enough, although I don't like the idea of binding project to one of those ides.
Assertions in the beginning of the function do look quite promising from my point of view.
Any other ideas?