quantifiedcode / python-anti-patterns

An open collection of Python anti-patterns and worst practices.
https://quantifiedcode.github.io/python-anti-patterns
Other
1.71k stars 249 forks source link

Using type() - fix examples to work, and demonstrate the principle. #142

Closed NickSto closed 4 years ago

NickSto commented 4 years ago

The existing example doesn't actually function as intended, and it doesn't really demonstrate the idea very well.

The reason stated for using isinstance() instead of comparing type()s is that isinstance() handles inheritance. So I'm not sure why the second example uses two unrelated classes Rectangle and Circle. The obvious choice is to use Rectangle and Square, as in this fix.

And I'm not sure what the idea was with the third example, either. The Rectangle object is not a ListType, and I'm not sure why one would think it is. So I changed it to be a repeat of the worst practice example, except using the best practice to compare.