简体   繁体   中英

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. 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 . 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 ):

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 .

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 :

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. If the list is part of an Item, I usually pass that item to another callback using meta parameter of a Request/Response object .

Just do self.commentList.append(someData)

(Note that normal Python style is to use comment_list and some_data , though.)

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.

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). 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").

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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