u/Commercial-Range-935

I'm attempting to build a lexer in Python and using a class.

# TOKEN TYPES

tokenConst = "tokenConst"
tokenID = "tokenID"
tokenInt = "tokenInt"
tokenVoid = "tokenVoid"
tokenReturn = "tokenReturn"
tokenOpPar = "tokenOpPar"
tokenCloPar = "tokenCloPar"
tokenOpBrace = "tokenOpBrace"
tokenCloBrace = "tokenCloBrace"
tokenSemiCol = "tokenSemiCol"


class Token:
    
def __init__(self, tclass, value):
        self.tclass = tclass
        self.value = value
    
def __repr__(self):
        if self.value:
            return f"{self.tclass}:{self.value}"
        else:
            return f"{self.tclass}"
class Lexer:
    def __init__(self, text):
        self.text = text
        self.pos = -1
        self.currentChar = None
        self.advance()

    def advance(self):
        self.pos += 1
        if self.pos < len(self.text):
            self.currentChar = self.text[self.pos]
        else:
            None


    
def tokenizeChar(self):
        tokens = []
        while self.currentChar != None:
            self.advance()
            if self.currentChar.isdigit():
                tokens.append(Token(tokenConst, self.currentChar))
            else:
                pass

        return tokens


def runLexer(text):
    lexer = Lexer(text)
    tokens = lexer.tokenizeChar

    return tokens

The first script is my class script, and the second script imports it and outputs the values.

My problem is that __repr__ is not returning the values, but rather the memory position, such as:
<lexer.Lexer object at 0x1005d5730>

How can I make it so that it actually returns the value instead of that?

Incredibly sorry for poor use of vocab or lack of further description 😅. I'll answer more questions if I can.

reddit.com
u/Commercial-Range-935 — 20 days ago