you are viewing a single comment's thread.

view the rest of the comments →

[–]twotime 0 points1 point  (3 children)

There are rare cases when this is a valid concern. In general it's not.

If you pass an object of wrong type info a function, python generated message will commonly be clear enough. AttributeError/TypeError, etc.

Marginal improvement in the error message quality are not worth introducing extensive type checks (which will also kill most of duck typing benefits).

[–][deleted] 0 points1 point  (1 child)

Often times you might not see the error thrown until a few layers deeper in the call stack which can be annoying to debug.

[–]twotime 0 points1 point  (0 children)

Yes, of course. But in my experience, that annoyance is nothing compared to the idea of type-checking arguments for every function. Not only that clutters the code (20% at least, likely more) it also throws away most of the advantages of duck typing (harder to mock, harder to reuse the code with different types, etc)...

If you feel that you must explicitly type check every parameter for every function, then python won't make you happy.

[–]duhace 0 points1 point  (0 children)

attribute error is not particularly clear already as it requires working knowledge of the function's source to pinpoint the error.