I am just beginning in python and I am getting the following error:
global name 'data' not defined on line# 62
Following is my code:
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)
Should I consider using instance variable instead of global. I have been mostly using Java and recently switched. Can anyone also provide some link for oops concepts in python.
dataat class level because it looked like the Java syntax for an instance field, and not because you would have used a static field in Java.__siftup()and__siftdown()as well.