Python 属性错误:';列表';对象没有属性';是空的';

Python 属性错误:';列表';对象没有属性';是空的';,python,Python,试图检查列表是否为空,继续获取属性错误,并且不确定如何修复?您定义了一个函数,而不是一个方法。那意味着你必须打电话 1 l = [1,2,3,4,5,6,7,3] 2 def is_empty(l): 3 """ 4 ------------------------------------------------------- 5 Determines if the list is empty. 6 Use:

试图检查列表是否为空,继续获取属性错误,并且不确定如何修复?

您定义了一个函数,而不是一个方法。那意味着你必须打电话

1   l = [1,2,3,4,5,6,7,3]
2   def is_empty(l):
3           """
4           -------------------------------------------------------
5           Determines if the list is empty.
6           Use: b = l.is_empty()
7           -------------------------------------------------------
8           Postconditions:
9               returns
10              True if the list is empty, False otherwise.
11          -------------------------------------------------------
12          """
13  
14          return len(l._values) == 0
15  
16  print("Empty?",l)
17  print(l.is_empty())
不是

def为空(输入列表):
如果类型(输入列表)=列表且len(输入列表)>0:
打印(“正在检查的列表的当前大小为{}”。格式(len(inputlist)))
返回错误
elif类型(输入列表)=列表和len(输入列表)==0:
打印(“列表为空!”)
返回真值
其他:
打印(“错误:输入必须是列表”)
testlist=[1,2,3,4]
空列表=[]
othervar=123
是否为空(测试列表)
是否为空(清空列表)
是否为空(其他变量)
输出:

def is_empty(inputlist):
    if type(inputlist) == list and len(inputlist) > 0:
        print("<dbg> The list you are checking has a current size of {}".format(len(inputlist)))
        return False
    elif type(inputlist) == list and len(inputlist) == 0:
        print("<dbg> The list is empty!")
        return True
    else:
        print ("<dbg> Error: input must be a list.")


testlist = [1,2,3,4]
emptylist = []
othervar = 123

is_empty(testlist)
is_empty(emptylist)
is_empty(othervar)
您正在检查的列表的当前大小为4
列表是空的!
错误:输入必须是列表。

是空的(l)
。您应该这样调用函数:
是空的(l)
而不是
l。顺便说一下,我认为
列表
没有
属性,即使它有,这很可能是您不应该依赖的实现细节,因此python列表没有任何属性值。只需使用
len(l)
请提供。退房,还有。
l.is_empty()
def is_empty(inputlist):
    if type(inputlist) == list and len(inputlist) > 0:
        print("<dbg> The list you are checking has a current size of {}".format(len(inputlist)))
        return False
    elif type(inputlist) == list and len(inputlist) == 0:
        print("<dbg> The list is empty!")
        return True
    else:
        print ("<dbg> Error: input must be a list.")


testlist = [1,2,3,4]
emptylist = []
othervar = 123

is_empty(testlist)
is_empty(emptylist)
is_empty(othervar)
<dbg> The list you are checking has a current size of 4
<dbg> The list is empty!
<dbg> Error: input must be a list.