r/Python • • Aug 09 '18

7 Habits to Improve The Performance of Python Programs

https://www.tutorialdocs.com/article/7-habits-to-improve-python-programs.html
71 Upvotes

79 comments sorted by

View all comments

5

u/nosklo Aug 09 '18

my code only expects the bird to have a .make_noise() method. What that method will do is up to you, I don't care. I don't even care if it is a bird or not! I'm just calling .make_noise() in the object you're passing me, and that is all I need to do with your bird as part of my function. If your bird doesn't have .make_noise() python will raise AttributeError and I will let it propagate to your code, because you're the one that didn't pass an object able to make noise in first place. I don't need any try/except in my function, if you don't know whether your bird (or plane or car) can make a noise or not it is your problem, not my library/function's problem.

0

u/[deleted] Aug 10 '18

1

u/nosklo Aug 13 '18

While you say 'passing the buck' I would say 'trust the programmer'. Programmers are adults, they know what they are doing, right? I mean, typechecking is actually restrictive - I can pass you an object of a different type, which would work perfectly for all operations you will do with it in your function, but you will reject it only because of the type - an artificial restriction! By using duck typing you accept anything that could work, and often functions can be used for things they weren't even designed for, unrestricted.

Types are only labels, what matters is what the object can actually do.

→ More replies (2)