331-2 - Python Data Analysis Basics - Calculating Artist Age

Hi,

I used isinstance rather than type, is there any reason why one is better than the other. Here is my code:

ages =
for row in moma:
date = row[6]
birth = row[3]
if isinstance(birth,int):
age = date - birth
else:
age = 0
ages.append(age)

final_ages =
for age in ages:
if age > 20:
final_age = age
else:
final_age = ‘Unknown’
final_ages.append(final_age)

It seemed to return the correct result.

Hi @jdj,

For the scope of this task, there is no difference which of them to use: both are correct approaches.

In general, however, there is a difference: isinstance() takes into account class inheritance, which means that a subclass of a class is an instance of that class. Instead, type() requires an absolute identity of the types and doesn’t take into account subclasses. In this article you’ll find a good example of their difference.

1 Like