AttributeError:' str'对象没有属性' typecode'

时间:2016-11-28 16:24:18

标签: python vsphere pysphere

我正在尝试在pysphere中使用VIProperty,但我正在' str'对象没有属性' typecode'

代码:

for h, mor in server.get_hosts().items():
    if mor == 'xxx.com':
        prop = VIProperty(server, mor)

错误:

Traceback (most recent call last):
  File "teardown.py", line 29, in <module>
    prop = VIProperty(server, mor)
  File "/usr/local/lib/python2.7/dist-packages/pysphere/vi_property.py", line 38, in __init__
    self._type = obj.typecode.type[1]
AttributeError: 'str' object has no attribute 'typecode'

1 个答案:

答案 0 :(得分:-1)

工作是因为“mor”stil字符串类型和'str'对象没有属性'typecode'

VIProperty

class VIProperty(object):
    def __init__(self, server, obj):
        self._server = server
        self._obj = obj
        self._values_set = False
        self._type = obj.typecode.type[1]

你的通话方式:

for h, mor in server.get_hosts().items():
    if mor == 'xxx.com':
        print type(mor) # <<<< 'str'
        prop = VIProperty(server, mor)

试试:

    hosts = server.get_hosts()
    for hmor, hname in hosts.items():
        if hname == 'xxx.com':
           p = VIProperty(server, hmor)