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.
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 comparingtype()
s is thatisinstance()
handles inheritance. So I'm not sure why the second example uses two unrelated classesRectangle
andCircle
. The obvious choice is to useRectangle
andSquare
, as in this fix.And I'm not sure what the idea was with the third example, either. The
Rectangle
object is not aListType
, 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.