Is it Pythonic to check function argument types?

Your taste may vary, but the Pythonic(tm) style is to just go ahead and use objects as you need to. If they don’t support the operations you’re attempting, an exception will be raised. This is known as duck typing.

There are a few reasons for favoring this style: first, it enables polymorphism by allowing you to use new kinds of objects with existing code so long as the new objects support the right operations. Second, it streamlines the successful path by avoiding numerous checks.

Of course, the error message you get when using wrong arguments will be clearer with type checking than with duck typing, but as I say, your taste may vary.

Leave a Comment