Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.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,我想在迭代任何属性时获取其名称 ts3defines.py看起来像: getItems(object)函数如下所示: 所讨论的代码如下所示: 我的问题是关于变量的。它不应该返回字符串VIRTUALSERVER\u BLA,等等吗 为什么它会导致 11/25/2017 16:07:44 pyTSon.PluginHost.infoData调用python插件的infoData时出错扩展信息:回溯(最近一次调用上次): infoData中第476行的文件“C:/Users/blusc/AppData

我想在迭代任何属性时获取其名称

ts3defines.py看起来像:

getItems(object)
函数如下所示:

所讨论的代码如下所示:

我的问题是关于
变量的。它不应该返回字符串
VIRTUALSERVER\u BLA
,等等吗

为什么它会导致

11/25/2017 16:07:44 pyTSon.PluginHost.infoData调用python插件的infoData时出错扩展信息:回溯(最近一次调用上次):
infoData中第476行的文件“C:/Users/blusc/AppData/Roaming/TS3Client/plugins/pyTSon/scripts\pluginhost.py”
数据=p.infoData(SCID、aid、atype)
infoData中第160行的文件“C:/Users/blusc/AppData/Roaming/TS3Client/plugins/pyTSon/scripts\info\\ uuuuu init\ uuuu.py”
返回self.getServerInfo(SCID)
文件“C:/Users/blusc/AppData/Roaming/TS3Client/plugins/pyTSon/scripts\info\\uuuu init\uuuu.py”,第148行,位于getServerInfo中
i、 追加(“{0}:{1}.”格式(var.\uuuu name\uuuu,var))
AttributeError:'int'对象没有属性'\u\u name\u'

我不明白你为什么试图访问
\uuuu name\uuuu
。您已经在
getItems
方法中拥有了该名称;它是
a
。您应该返回它并在循环中使用它

def getItems(object):
    return [(a, getattr(object, a)) for a in dir(object)
            if not a.startswith('__') and not callable(getattr(object, a))]


什么是
var
ts3.getServerVariable(schid,var)
返回值?一般来说,Python对象没有
\uuuuu name\uuuu
属性(例如
int
s),那么您为什么期望有一个属性呢?
def getItems(object):
    return [getattr(object, a) for a in dir(object)
                if not a.startswith('__') and not callable(getattr(object, a))]
for var in getItems(ts3defines.VirtualServerProperties):
    (err, var) = ts3.getServerVariable(schid, var)
    if err == ts3defines.ERROR_ok and var != "" and var != 0:
        i.append('{0}: {1}'.format(var.__name__, var))
def getItems(object):
    return [(a, getattr(object, a)) for a in dir(object)
            if not a.startswith('__') and not callable(getattr(object, a))]
for name, var in getItems(ts3defines.VirtualServerProperties):
    (err, var) = ts3.getServerVariable(schid, var)
    if err == ts3defines.ERROR_ok and var != "" and var != 0:
        i.append('{0}: {1}'.format(name, var))