Miscellaneous
- import sys
- import re
- from typing import Match, AnyStr, IO
-
- # "typing.Match" describes regex matches from the re module
- x: Match[str] = re.match(r'[0-9]+', "15")
-
- # Use IO[] for functions that should accept or return any
- # object that comes from an open() call (IO[] does not
- # distinguish between reading, writing or other modes)
- def get_sys_IO(mode: str = 'w') -> IO[str]:
- if mode == 'w':
- return sys.stdout
- elif mode == 'r':
- return sys.stdin
- else:
- return sys.stdout
-
- # Forward references are useful if you want to reference a class before
- # it is defined
- def f(foo: A) -> int: # This will fail
- ...
-
- class A:
- ...
-
- # If you use the string literal 'A', it will pass as long as there is a
- # class of that name later on in the file
- def f(foo: 'A') -> int: # Ok
- ...