Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法构造python类_Python - Fatal编程技术网

无法构造python类

无法构造python类,python,Python,我有以下函数,它们在Python脚本的边界内工作 loop\u vCenter函数将返回带有cluster:host的词汇表 subs_per_socket函数将返回esx的套接字数 def loop_vcenters(vcenters): #------------------------- connect to vcenter ---------------------------------------- si = SmartConnect(host = vcenters,

我有以下函数,它们在Python脚本的边界内工作

loop\u vCenter
函数将返回带有
cluster:host的词汇表
subs_per_socket
函数将返回esx的套接字数

def loop_vcenters(vcenters):
    #------------------------- connect to vcenter ----------------------------------------
    si = SmartConnect(host = vcenters,user = 'username',pwd = 'password' ,sslContext=context)
    # disconnect from vcenter one done
    atexit.register(Disconnect, si)
    #get the object content from vcenter
    content = si.RetrieveContent()
    #list clusters
    cluster_host_dic_list=[]
    cluster_name = ""
    for cluster_obj in get_obj(content, vim.ComputeResource):
        cluster=cluster_obj.name
        hosts=[]
        for host in cluster_obj.host:
            hosts.append(host.name)
        #create dictinary ,key=cluster value=esx
        cluster_dic={cluster:hosts}
        cluster_host_dic_list.append(cluster_dic)
    return cluster_host_dic_list

def subs_per_socket(host):
    shost = content.searchIndex.FindByDnsName(dnsName=host, vmSearch=False)
    socket_count = shost.summary.hardware.numCpuPkgs
    sub_per_socket = socket_count/2
    return sub_per_socket
我想把这两个函数都放到一个类中,但我不知道如何实现

class loop_vcenters:    
    def hosts_dic(self,name):
        si = SmartConnect(host = vcenters,user = 'username',pwd = 'password' ,sslContext=context)
        atexit.register(Disconnect, si)
        content = si.RetrieveContent()
        cluster_host_dic_list=[]
        cluster_name = ""
        for cluster_obj in get_obj(content, vim.ComputeResource):
            cluster=cluster_obj.name
            hosts=[]
            for host in cluster_obj.host:
                hosts.append(host.name)
            cluster_dic={cluster:hosts}
            cluster_host_dic_list.append(cluster_dic)
      return cluster_host_dic_list
  • 我无法像返回的
    循环\u vCenter
    函数那样获取主机字典
  • 如何将
    subs\u per\u socket(host)
    函数添加到类中
  • 当定义了noinit时,您正在使用参数调用类loop\u vCenter

    文件“”,第5行,在\uuuuu init\uuuu

    名称错误:未定义全局名称“vCenter”

    如果要将参数传递给host_dic,则应调用

    d = loop_vcenters.host_dic('vcenters')
    
    这将把集群主机dic列表返回给变量d


    要添加Sub_per_socket(host),只需在类下定义它,就像在其他函数中一样。

    循环\u vCenter
    函数中,您使用了参数
    vCenter
    ,但
    主机dic
    使用
    名称。因此,对于Python来说,
    vCenter
    变量必须来自外部(在本例中为全局)范围
    d = loop_vcenters('vcenters')
    
    d = loop_vcenters.host_dic('vcenters')