From the course: Learning SOLID Programming Principles
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Avoiding isinstance() - Python Tutorial
From the course: Learning SOLID Programming Principles
Avoiding isinstance()
- [Instructor] Generally, the isinstance() function should be used rarely, if at all. When creating classes, for which Liskov Substitution works properly, there should never be a need to identify the type of an object, since nothing is absolute. I don't really mean never. I mean, almost never for an extremely narrow band of almost. This is something I see once in a while. We rarely need to test types like this with a check for a specific type. First of all, this breaks ordinary duck typing, a class that also implements the Reader protocol but is not a direct subclass of Reader, can't be used here. Second, it's much easier to include type annotation and then turn tools like mypy loose on the code to check those annotations. Here's the same code with type annotations. You'll notice I've used them in all the examples. The mypy tool can check the code to be sure the Readers and Correlations are collaborating properly. In rare,…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.