python中的全局变量出错

python中的全局变量出错,python,Python,我刚刚开始使用python,出现以下错误: global name 'data' not defined on line# 62 以下是我的代码: class PriorityQueue(object): """ Max PriorityQueue Binary heap based implementation """ data = [] def __init__(self): pass def insert(sel

我刚刚开始使用python,出现以下错误:

global name 'data' not defined on line# 62
以下是我的代码:

class PriorityQueue(object):
    """
    Max PriorityQueue
    Binary heap based implementation
    """
    data = []

    def __init__(self):
        pass

    def insert(self,d):
        global data
        data.append(d)
        __siftup()

    def extractMax(self):
        global data
        max = -1

        if not len(data) == 0:
            max = data[0]

        __siftdown()
        return max

    def __siftup():
        global data
        index = len(data) - 1
        pIndex = (index - 1)/2

        while pIndex > 0 and data[pIndex] > data[index]:
            data[pIndex], data[index] = data[index], data[pIndex]
            index = pIndex
            pIndex = (index - 1)/2

    def __siftdown():
        global data
        data[0] = data.pop()
        index = 0

        while index *2 <= len(data) -1:
            minIndex = __getMinIndex(index)
            if data[minIndex] > data[index]:
                data[minIndex], data[index] = data[index], data[minIndex]
                index = minIndex
            else:
                break

    def __getMinIndex(i):
        global data
        lIndex = i*2 +1
        rIndex = lIndex + 1

        if rIndex >= len(data) or data[lIndex] > data[rIndex]:
            return lIndex
        else:
            return rIndex

    """Test Script"""

    q = PriorityQueue()
    q.insert(3)

我应该考虑使用实例变量而不是全局变量。我大部分时间都在使用Java,最近换了。任何人也可以为python中的oops概念提供一些链接

使用实例变量Luke。

使用实例变量Luke。

当您编写:

class MyClass(object):
    data = []
数据既不是通过该类生成的对象的实例变量,也不是模块范围中的全局变量,而是类级别的静态变量

>> instance = MyClass()
>> instance.data
[]
>> instance.data.append(1)
>> instance.data
[1]
>> other_instance = MyClass()
>> other_instance.data
[1]
这通常不是你想要的

创建实例变量的正确方法是在_init__构造函数中分配它们

class PriorityQueue(object):    
    def __init__(self):
        self.data = []
然后,在其他方法中,您可以将数据作为self.data引用,而不是尝试使用global语句。事实上,一般情况下,尽量避免使用globals。

在编写时:

class MyClass(object):
    data = []
数据既不是通过该类生成的对象的实例变量,也不是模块范围中的全局变量,而是类级别的静态变量

>> instance = MyClass()
>> instance.data
[]
>> instance.data.append(1)
>> instance.data
[1]
>> other_instance = MyClass()
>> other_instance.data
[1]
这通常不是你想要的

创建实例变量的正确方法是在_init__构造函数中分配它们

class PriorityQueue(object):    
    def __init__(self):
        self.data = []
然后,在其他方法中,您可以将数据作为self.data引用,而不是尝试使用global语句。事实上,一般情况下尽量避免使用globals。

也许你会觉得有用。特别是LGB部分和以下章节

这两个小例子总是帮助我消除困惑

#!/usr/bin/python
# -*- coding: utf-8 -*-

class A(object):
    def __init__(self, a):
        self.a = a

    def f(self, v):
        return self.a + v

class B(A):
    def __init__(self, a, b):
        A.__init__(self,a)
        self.b = b

    def f(self, v):
        return A.f(self,v)*self.b

if __name__ == '__main__':
    b = B(2,3)
    print "18",b.f(4)
    b.a = 3
    print "21",b.f(4)
结果是:

18 18
21 21
[Finished in 0.1s]
1
2
2
[Finished in 0.3s]
第二个:

n = 1

def A():
   n = 2
   def B():
       print n
   B()

def func1():
    n = 2
    print n

def func2():
    global n
    n = 2
    print n


if __name__ == '__main__':
    func1()
    func2()
    A()
结果是:

18 18
21 21
[Finished in 0.1s]
1
2
2
[Finished in 0.3s]
也许你会觉得很有帮助。特别是LGB部分和以下章节

这两个小例子总是帮助我消除困惑

#!/usr/bin/python
# -*- coding: utf-8 -*-

class A(object):
    def __init__(self, a):
        self.a = a

    def f(self, v):
        return self.a + v

class B(A):
    def __init__(self, a, b):
        A.__init__(self,a)
        self.b = b

    def f(self, v):
        return A.f(self,v)*self.b

if __name__ == '__main__':
    b = B(2,3)
    print "18",b.f(4)
    b.a = 3
    print "21",b.f(4)
结果是:

18 18
21 21
[Finished in 0.1s]
1
2
2
[Finished in 0.3s]
第二个:

n = 1

def A():
   n = 2
   def B():
       print n
   B()

def func1():
    n = 2
    print n

def func2():
    global n
    n = 2
    print n


if __name__ == '__main__':
    func1()
    func2()
    A()
结果是:

18 18
21 21
[Finished in 0.1s]
1
2
2
[Finished in 0.3s]

我希望您将数据放在类级别上,因为它看起来像是实例字段的Java语法,而不是因为您会在Java中使用静态字段。一旦修复该错误,您将遇到u siftup和u siftdown问题。我希望您将数据放在类级别上,因为它看起来像是实例字段的Java语法,并不是因为你会在Java中使用一个静态字段,一旦你修复了这个错误,你也会遇到u siftup和u siftdown的问题。