title: How can I get the root/parent class of an object?
body: <p>How can I check the root/parent class of an object? e.g</p>
  
  <pre><code>from PIL import Image as im
  x = im.open('test.png')
  
  type(x)
  Out[]: PIL.PngImagePlugin.PngImageFile
  
  #pseudocode:
  isinstance_parent(x, PIL)
  Out[]: True
  </code></pre>
  
  <p>I have a list which contains PIL image objects of different types (PNG, JPEG etc.). I want to be able to check which are PIL image objects so I can close them later in the script (so it will allow me to delete them).</p>
  
  <p>I've searched quite a bit but couldn't make any answers work for me. Most point to using some variance of isinstance(object, type) but that requires being very specific:</p>
  
  <pre><code>isinstance(x, PIL.PngImagePlugin.PngImageFile)
  Out[]: True
  </code></pre>
  
  <p>But the images could be one of many types JPG, GIF, PNG etc. I'd ideally like just to be able to see if belongs to the PIL class.</p>
  
  <p>I've thought of hacky ways to do it... just make a list of every image it could be and see how PIL classifies them. </p>
  
  <p>Or something like this:</p>
  
  <pre><code>str(type(x))
  Out[80]: "&lt;class 'PIL.PngImagePlugin.PngImageFile'&gt;"
  
  if str(type(x)).split()[1].startswith('\'PIL'):
      print('It is an image')
  </code></pre>
  
  <p>But I feel like there should be a more pythonic way that I'm missing right?</p>
  
  <p>Cheers</p>