检查 PyVmomi 对象是否属于 python 中的某个 class

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

我正在尝试弄清楚如何检查一个对象是否属于特定的 class,它对 class 类(如“str”、“int”等)工作正常,但不适用于来自模块的自定义 classes。

这是我试过的。

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]: 

通过使用isinstance,它可以做你想做的事。
例如,如果一个对象是一个集群,这里使用 isinstance 来检查。

#!/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 = 'administrator@vsphere.local'
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)