0

当我尝试为反转计数算法实现一个简单的类时,为什么 PyDev 会给我这个错误:“类变量未定义”?这是我的代码:

from collections import deque

Class inversionCount:

    def __init__(self, n):
        self.n = n
    def inversionMergeSort(m, n):
        print m
        if len(m) <= 1:
            n = 0
            return (m, n)
        half = len(m)/2
        left = m[0:half]
        right = m[half:]
        left = mergeSort(left)
        right = mergeSort(right)
        return inversionSort(left, right)

    def inversionSort(left, right, n):
        leftQueue = deque(i for i in left)
        rightQueue = deque(j for j in right)
        orderedList = []
        while len(leftQueue) > 0 or len(rightQueue) > 0:
            if len(leftQueue) > 0 and len(rightQueue) > 0:
                if leftQueue[0] < rightQueue[0]:
                    orderedList.append(leftQueue[0])
                    leftQueue.popleft()
                else:
                    orderedList.append(rightQueue[0])
                    if len(leftQueue) > 1:
                        self.n += len(leftQueue)
                    rightQueue.popleft()
            elif len(leftQueue) > 0:
                orderedList.append(leftQueue[0])
                leftQueue.popleft()
            elif len(rightQueue) > 0:
                orderedList.append(rightQueue[0])
                rightQueue.popleft()
        return (orderedList, n)

然而 PyDev 没有认识到 inversionCount 确实是一个类。有什么想法吗?

4

1 回答 1

9

class是小写:

class inversionCount:

抛出错误是因为变量Class确实没有定义;python 区分大小写,与Class; 不同class。前者被视为变量名,而被视为class关键字。

如果你用解释器运行你的代码,它会抛出一个语法错误;PyDev 的解释略有不同。

于 2012-11-06T16:20:51.580 回答