print dir(XXX) 给出空白属性

print dir(XXX) gives out blank attributes

简而言之,我使用 xlsx 文件并在使用 print dir(alist) 检查某些列表时得到空白属性。

neglist = neglist.tolist()

此时我想检查evth是否正常:

def check_variab (variab):
    print "The type is %s" % type(variab)
    print "Its length = %i" % len(variab)
    print "Its attributes are:" % dir(variab)

print 'neglist'
check_variab(neglist)

但我得到的是:

type: list
length: 19
attributes: 

虽然类型是list,但是没有打印属性,它的长度和内容都可以。

谁能解释为什么会这样?

您忘记使用 %s 占位符,因此 不会插入任何内容。添加 %s%r

print "Its attributes are %s:" % dir(variab)
#                         ^^ A placeholder for the value

如果没有该占位符,您将看不到任何东西,确实如此:

>>> variab = ['foo', 'bar']
>>> dir(variab)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>> print "Its attributes are:" % dir(variab)
Its attributes are:
>>> print "Its attributes are %s:" % dir(variab)
Its attributes are ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']:

您想在列表中使用 standard sequence operations