全局变量在函数中没有识别

问题描述:

我知道这个问题看起来和其他很多其他的一样,因为我只是读了所有的东西,他们都说要做我已经尝试过的东西,我错过了与我的情况有细微差别)。这是我的情况:全局变量在函数中没有识别

我正在使用Scrapy和Python 2.7.11编写一个刮板,我的代码看起来像这样(这是一个复制和粘贴,省略了不相关的行,但我可以根据请求重新添加它们) :

class LbcSubtopicSpider(scrapy.Spider): 

    ...omitted... 

    rawTranscripts = [] 
    rawTranslations = [] 

    def parse(self, response): 
     #global rawTranscripts, rawTranslations 
     rawTitles = [] 
     rawVideos = [] 
     for sel in response.xpath('//ul[1]'): #only scrape the first list 

     ...omitted... 

      index = 0 
      for sub in sel.xpath('li/ul/li/a'): #scrape the sublist items 
       index += 1 
       if index%2!=0: #odd numbered entries are the transcripts 
        transcriptLink = sub.xpath('@href').extract() 
        #url = response.urljoin(transcriptLink[0]) 
        #yield scrapy.Request(url, callback=self.parse_transcript) 
       else: #even numbered entries are the translations 
        translationLink = sub.xpath('@href').extract() 
        url = response.urljoin(translationLink[0]) 
        yield scrapy.Request(url, callback=self.parse_translation) 

     print rawTitles 
     print rawVideos 
     print rawTranslations 

    def parse_translation(self, response): 
     global rawTranslations 
     for sel in response.xpath('//p[not(@class)]'): 
      rawTranslation = sel.xpath('text()').extract() 
      rawTranslations.append(rawTranslation) 

这将返回一个错误的任何时间“打印rawTranslations”还是因为全球“rawTranslations”没有定义“rawTranslations.append(rawTranslation)”之称。

正如我之前说过的,我已经深入研究了这个问题,并且几乎所有互联网上的人都说要在任何函数的开始处添加一个“全局(名称)”行来使用/修改它虽然我没有分配过它,所以我甚至不需要这个)。无论我的全局行被注释掉了,我都会得到相同的结果。这种行为似乎违背了我读过的有关全局变量在Python中的工作原理的所有内容,所以我怀疑这可能是一种Scrapy怪癖,与通过scrapy.Request(....)调用解析函数的方式有关。

道歉发布似乎是你再次看到的同样的问题,但它似乎有点扭曲这一次,希望有人可以得到它的底部。谢谢。

+0

它看起来像你的缩进是关闭的,例如函数'parse'肯定是类“LbcSubtopicSpider”的一个方法?另外它看起来像rawTranslations不是一个全局属性,而是一个类属性。当然,解决方案是使用'self.rawTranslations'或'LbcSubtopicSpider.rawTranslations'(取决于上下文)。 – syntonym

+0

当我将它粘贴在这里时,缩进实际上被破坏 - 将修复它。至于全球/类别属性点:我想我甚至没有考虑过会有什么区别。这可能是大部分使用Java编写代码并在类中做所有事情的产物...... – jah

+0

'rawTranslations'不是全局的,它是一个类变量... – MisterMiyagi

在你的情况下,你想要访问的变量不是全局的,它在类的范围内。

global_var = "global" 

class Example: 

    class_var = "class" 

    def __init__(self): 
     self.instance_var = "instance" 

    def check(self): 
     print(instance_var) # error 
     print(self.instance_var) # works 
     print(class_var) # error 
     print(self.class_var) # works, lookup goes "up" to the class 
     print(global_var) # works 
     print(self.global_var) # works not 

如果你想要写一个全局变量你只需要global关键字。提示:不要这样做,因为写入的全局变量只会带来痛苦和绝望。只使用全局变量作为(config)常量。

global_var = "global" 

class Example: 

    def ex1(self): 
     global_var = "local" # creates a new local variable named "global_var" 

    def ex2(self): 
     global global_var 
     global_var = "local" # changes the global variable 

Example().ex1() 
print(global_var) # will still be "global" 
Example().ex2() 
print(global_var) # willnow be "local" 

,如果你想使用类变量,你可以使用self.xxx

class A: 
...  var = [] 
...  def test(self): 
...   self.var.append(10) 
...   print self.var