python 3.x - putting current class as return type annotation -
in python 3 can make arguments , return type annotations. example:
class graph: def __init__(self, v: int, e: int, edges: list): pass @classmethod def fromfile(cls, readobj: type(sys.stdin)): pass def v(self) -> int: pass def e(self) -> int: pass
the problem can't make annotation return type of current class (graph), not defined yet. example:
class graph: def reverse(self) -> graph: pass
this code goes error
def reverse(self) -> graph: nameerror: name 'graph' not defined
these annotations useful both documenting , allowing ide recognize argument , return types => enable autocomplete
upd: came either impossible or requires hacks don't like, decided use def reverse (self) -> 'graph':
understandable documentation although breaks rule. downside doesn't work ide autocomplete.
so after while can decision took using -> 'graph'
instead of -> graph
. not make ide (pycharm) able recognize type way works enough documentation purposes.
another possible solution use changing annotation @ runtime doesn't solve problem documentation - won't want type declarations somewhere in middle of sources...
the problem has roots in recognizing class object before class defined. impossible in python.
Comments
Post a Comment