为什么这段代码抛出“'list'对象没有属性”错误

时间:2017-11-04 19:15:19

标签: python python-3.x

为什么抛出

  

queue_length = items_in_queue.get_jenkins_queue_len(items_in_queue)   AttributeError:'list'对象没有属性'get_jenkins_queue_len'

class JenkinsQueue :

    def get_jenkins_items_in_queue(self) :
        logger.info("Starting...")
        with open('/Users/praveen-bhasker/datafile.json', encoding='utf-8') as data_file:
            data = json.loads(data_file.read())
            return data["items"]


    def get_jenkins_queue_len(self, data):
        print(data)
        return


jenkinsqueue = JenkinsQueue()
items_in_queue = jenkinsqueue.get_jenkins_items_in_queue()
queue_length = items_in_queue.get_jenkins_queue_len(items_in_queue)

1 个答案:

答案 0 :(得分:2)

由于get_jenkins_queue_len()是在JenkinsQueue类中实现的,因此您需要调用该类的对象,因此您必须更换 queue_length = items_in_queue.get_jenkins_queue_len(items_in_queue)jenkinsqueue.get_jenkins_queue_len(items_in_queue)