Python 为什么我的功能没有定义

Python 为什么我的功能没有定义,python,object,Python,Object,我是python新手,对于我的一生来说,我无法找出为什么或者如何定义我的函数。这可能是一个愚蠢的问题,我道歉,但我真的被卡住了,无法测试/修复其余部分,直到我让这个部分工作。感谢您的帮助。 这是我调用scanner的主类,但我的问题是为什么不调用getChar()。堆栈跟踪的最深处告诉我没有定义nextChar=getChar() from Scanner import scanner from Constants import * def main(): python = scanner

我是python新手,对于我的一生来说,我无法找出为什么或者如何定义我的函数。这可能是一个愚蠢的问题,我道歉,但我真的被卡住了,无法测试/修复其余部分,直到我让这个部分工作。感谢您的帮助。 这是我调用scanner的主类,但我的问题是为什么不调用getChar()。堆栈跟踪的最深处告诉我没有定义nextChar=getChar()

from Scanner import scanner
from Constants import *

def main():
  python = scanner()
  token = python.scanner()
  while token.tokenType != END_OF_FILE:
      print(token.tokenType, " ", token.lexeme)
      token = python.scanner()


main()




class TokenRec(object):

def __init__(self, tokenType, lexeme, line, col):
    self.tokenType = tokenType
    self.lexeme = lexeme
    self.line = line
    self.col = col


class scanner():


# Constructor for the Scanner class
def __init__(self):
    self.fileName = input("Enter the file name: ")
    self.infile = open(self.fileName, "r")
    self.fChar = self.infile.read(1)
    self.line = 0
    self.col = 0

# gets the next character out of the file
def getChar():
    nextChar = file.read(1)
    if nextChar == "":
        nextChar = '\34'

    return nextChar


# adds the next character onto the lexeme buffer
def addChar(nextToken, nextChar):
    nextToken.lexeme += nextChar


def isKeyWord(nextChar):

    return True

def isSingleToken(nextChar):

    return True

def isMultiToken(nextChar):

    return True


def scanner(self):

    while True:

        nextToken = TokenRec("","",self.line,self.col)
        nextChar = getChar()
        if nextChar == '\n':
            self.line += 1
        self.col = 0

        if nextChar.isalpha():
            addChar(nextToken, nextChar)
            nextChar = getChar()

            while nextChar != " ":
                nextToken.lexeme += nextChar
                nextChar = getChar()
            if nextChar.issspace():
                if isKeyWord(nextChar):
                    print("Error")
                    #Part 2
                else:
                    nextToken.tokenType = 33

        elif nextChar.isdigit():
            nextToken.lexeme = nextChar
            nextChar = getChar()
            while nextChar != " ":
                nextToken.lexeme += nextChar
                nextChar = getChar()
            nextToken.tokenType = 75

        elif nextChar is '"':
            nextChar = getChar()
            while nextChar != '"':
                nextToken.lexeme += nextChar
                nextChar = getChar()

        elif isSingleToken(nextChar):
            print("Error")
            # Part 2

        elif nextChar is '#':

            comment = file.readline()

        elif nextChar is None:
            nextToken.tokenType = 99
            nextToken.lexeme = "END OF FILE"

        else:
            print("Character is illegal or unknown")

在发布这篇文章几分钟后,我找到了我的答案。我所要做的就是将getChar()定义为scanner.getChar()。如果这不是正确的方法,或者我仍然缺少一些东西,请随时提供帮助。

问题可能是您的函数是在另一个类中定义的

例如:

>>> class foo:
...     @staticmethod
...     def bar():
...             print 'foo+bar = foobar'
... 
>>> bar()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'bar' is not defined
>>> foo().bar()
foo+bar = foobar
>>> 
>>类foo:
...     @静力学方法
...     def bar():
...             打印“foo+bar=foobar”
... 
>>>bar()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
NameError:未定义名称“bar”
>>>foo().bar()
foo+bar=foobar
>>> 

请发布错误的完整堆栈跟踪!谢谢是的,请发布完整的堆栈跟踪,同时,请不要忘记缩进<代码>回溯(最后一次调用):文件“/Users/diessner/Documents/beneditine/Fall_2014/CMSC 385/Project2/Driver.py”,第17行,在main()文件“/Users/diessner/Documents/beneditine/Fall_2014/CMSC 385/Project2/Driver.py”,第11行,在main-token=python.scanner()文件中“/Users/diessner/Documents/Benedictine/Fall_2014/CMSC 385/Project2/Scanner.py”,第55行,在Scanner nextChar=getChar(self)中NameError:name'getChar'未定义我认为这是因为我没有像下面的答案那样详细说明是什么,或者我在我发布的答案中所说的那样找到了什么。有时它只是帮助写出来并解释它。哦,天哪,我还没有看到你的答案,很高兴你找到了!