Check if a PyVmomi object is of a certain class in python

I am trying to figure out how to check if an object is of a specific class, it works just fine for classes like “str”, “int” etc but not for custom classes from modules.

Here is what I have tried.

In [36]: type(cluster)                                                                                                                                                                                 
Out[36]: pyVmomi.VmomiSupport.vim.ComputeResource

In [37]: type(cluster) == pyVmomi.VmomiSupport.vim.ComputeResource                                                                                                                                     
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/source/modregk_bga/eti-data-pipelines/datapipes/lm-sql-capacity/libs/vcenser.py in <module>
----> 1 type(cluster) == pyVmomi.VmomiSupport.vim.ComputeResource

AttributeError: module 'pyVmomi.VmomiSupport' has no attribute 'vim'

In [38]: type ("str")                                                                                                                                                                                  
Out[38]: str

In [39]: type ("str") == str                                                                                                                                                                           
Out[39]: True

In [40]: isinstance(cluster.summary, pyVmomi.VmomiSupport.vim.ComputeResource.Summary)                                                                                                                 
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/source/modregk_bga/eti-data-pipelines/datapipes/lm-sql-capacity/libs/vcenser.py in <module>
----> 1 isinstance(cluster.summary, pyVmomi.VmomiSupport.vim.ComputeResource.Summary)

AttributeError: module 'pyVmomi.VmomiSupport' has no attribute 'vim'

In [41]: 

Answer

By using isinstance, it can do what you want to do.
For example, here’s to check using isinstance if an object is a cluster.

#!/usr/bin/env python
from pyVim.connect import SmartConnect, Disconnect
from pyVmomi import vim
import ssl
import atexit

host = 'change me to vCenter IP or hostname'
username = '[email protected]'
password = 'change me to user password'
mob = vim.ComputeResource

if __name__ == "__main__":
    context = None
    if hasattr(ssl, '_create_unverified_context'):
        context = ssl._create_unverified_context()

    si = SmartConnect(host=host,
                      user=username,
                      pwd=password,
                      sslContext=context)

    atexit.register(Disconnect, si)

    content = si.content

    mob_list = content.viewManager.CreateContainerView(content.rootFolder,
                                                       [mob],
                                                       True)

    for mob in mob_list.view:
        # Check if an object is the cluster
        if isinstance(mob, vim.ComputeResource):
            print("%s is Cluster object" % mob.name)