简体   繁体   English

如何在python中定义全局列表

[英]How to define a global list in python

I have two methods that should write to the same list. 我有两个方法应该写入同一个列表。

class MySpider():

    def parse_post(self, response):
        commentList = []
        ...  
        commentList.append(someData)

    def parse_comments(self, response):
        commentList = []
        ...  
        commentList.append(someData)

In this code there are two commentList lists but I need a single list where I can append data. 在这段代码中有两个commentList列表,但我需要一个列表,我可以在其中附加数据。 I want to access this list in any method of this class. 我想在这个类的任何方法中访问此列表。 I tried with 我试过了

class MySpider():

    commentNum = []

    def parse_post(self, response):
        ...  
        commentList.append(someData)

    def parse_comments(self, response):
        ...  
        commentList.append(someData)

But this gives me an error global name commentList is not defined . 但这给了我一个错误global name commentList is not defined Any ideas how to have a single list that can be accessed in all methods in that class? 有关如何在该类的所有方法中访问的单个列表的任何想法?

One way is to simply refer to the variable by its full name ( MySpider.commentList ): 一种方法是简单地通过其全名( MySpider.commentList )引用变量:

class MySpider(object):

    commentList = []

    def parse_post(self, response):
        ...  
        MySpider.commentList.append(someData)

    def parse_comments(self, response):
        ...  
        MySpider.commentList.append(someData)

This way all instances of MySpider will share the same variable . 这样, MySpider所有实例MySpider将共享相同的变量

If you might have multiple instances of MySpider , and want each instance to have its own commentList , then simply create it in the constructor and refer to it as self.commentList : 如果您可能有多个MySpider实例,并希望每个实例都有自己的commentList ,那么只需在构造函数中创建它并将其称为self.commentList

class MySpider(object):

    def __init__(self):    
        self.commentList = []

    def parse_post(self, response):
        ...  
        self.commentList.append(someData)

    def parse_comments(self, response):
        ...  
        self.commentList.append(someData)

If both versions would work in your case, I'd suggest using the latter. 如果两个版本都适用于您的情况,我建议使用后者。

Looks like you are using Scrapy. 看起来你正在使用Scrapy。 If the list is part of an Item, I usually pass that item to another callback using meta parameter of a Request/Response object . 如果列表是Item的一部分,我通常使用Request / Response对象的meta参数将该项传递给另一个回调。

Just do self.commentList.append(someData) 只需做self.commentList.append(someData)

(Note that normal Python style is to use comment_list and some_data , though.) (请注意,正常的Python样式是使用comment_listsome_data 。)

Just make it a instance attribute: 只需将其设为实例属性:

class MySpider(object):
    def __init__(self):
        self.comment_list = []
    def parse_post(self, response):
        ...  
        self.comment_list.append(someData)

The instance ( self in python by convention, this in java eg) is explicit in python. 实例( self在python按照惯例, this在Java EG)是蟒蛇明确。

If you initialize your array outside methods (like in your 2nd listing), you make it a class attribute (ie static one), that is "global" to all instances, and it should be referenced using the full name MySpider.comment_list or type(self).comment_list if you want to avoid the class name (eg for inheritance). 如果在方法之外初始化数组(比如在第二个列表中),则将其设置为类属性(即静态属性),对所有实例都是“全局”,并且应使用全名MySpider.comment_listtype(self).comment_list引用它type(self).comment_list如果你想避免类名(例如继承)。 Due to the lookup of attributes, self.comment_list will also work (if the attribute is not found at instance level, the class is looked for) but the distinction is less obvious ("explicit is better than implicit"). 由于查找属性, self.comment_list也可以工作(如果在实例级别找不到属性,则查找该类)但区别不太明显(“显式优于隐式”)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM