如何提示方法参数的自定义类型,其中类型和方法都在同一个类中?

问题描述 投票:0回答:1

如何提示函数

symbol
中的
add_symbol
参数是
Lexer.symbol
的类型?它说它没有定义。我尝试了一些方法,但我不知道什么会起作用。

class Lexer:
    class symbol:
        def __init__(self, lexeme: str, token: str):
            self.lexeme: str = lexeme
            self.token: str = token

    class SymbolTable:
        def __init__(self):
            self.symbols: list[Lexer.symbol] = [] # This works
            # self.symbols: list[symbol] = [] # This does not

        def add_symbol(self, symbol: Lexer.symbol): # This says undefined
            pass

        def add_symbols(self, symbol: list[symbol]): # This too
            pass

我应该在

symbol
类之外声明
Lexer
类吗?不过我确实希望它在里面。非常感谢。

我尝试了使用和不使用 Lexer 类来访问符号,但它仍然说它是未定义的。

python methods types parameters
1个回答
0
投票

您可以尝试添加

from __future__ import annotations
到文件开头

© www.soinside.com 2019 - 2024. All rights reserved.