Last active
April 13, 2024 20:42
-
-
Save mahenzon/1d6c50b30a2c9bb68bedea6d0a412662 to your computer and use it in GitHub Desktop.
Python Final and @ final examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Final, Sequence | |
ADMIN_IDS: Final[Sequence[int]] = [1, 5] | |
# ADMIN_IDS: Final[Sequence[int]] = (1, 5) | |
# ADMIN_IDS: Final[tuple[int, ...]] = (1, 5, 8) | |
def main() -> None: | |
print(ADMIN_IDS) | |
ADMIN_IDS.append(7) | |
print(ADMIN_IDS) | |
if __name__ == "__main__": | |
main() | |
# ADMIN_IDS = [1, 2, 3, 4, 5, 6, 7, 8, 9] | |
# main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Final | |
# wrong! | |
def example(value: Final[int]) -> None: | |
print(value) | |
def main() -> None: | |
example(123) | |
example("foobar") | |
if __name__ == "__main__": | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Final | |
PI: Final = 3.14 | |
def circumference(diameter: float) -> float: | |
return diameter * PI | |
def main() -> None: | |
print(circumference(3)) | |
print(circumference(5)) | |
if __name__ == "__main__": | |
main() | |
PI = 3 | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[tool.mypy] | |
python_version = "3.12" | |
pretty = true | |
strict = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mypy==1.9.0 | |
mypy-extensions==1.0.0 | |
typing_extensions==4.11.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from abc import ABC, abstractmethod | |
from typing import final | |
class Car: | |
pass | |
class SuperCarBase(ABC): | |
@abstractmethod | |
def race(self) -> None: | |
raise NotImplementedError | |
@final | |
class SuperCar(SuperCarBase): | |
def race(self) -> None: | |
print("Wrooom") | |
# class SuperDuperCar(SuperCar): | |
# pass | |
def main() -> None: | |
# print(SuperDuperCar()) | |
# print(SuperDuperCar.mro()) | |
print(SuperCar()) | |
print(SuperCar.mro()) | |
if __name__ == "__main__": | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import final | |
class BaseConfig: | |
def __init__(self) -> None: | |
self.debug = False | |
self.foo = "bar" | |
@final | |
def as_dict(self) -> dict[str, str]: | |
return vars(self) | |
class Config(BaseConfig): | |
def as_dict_new(self) -> dict[str, str]: | |
return vars(self) | {"version": "2.0"} | |
def main() -> None: | |
bc = BaseConfig() | |
print(bc.as_dict()) | |
c = Config() | |
print(c.as_dict()) | |
print(c.as_dict_new()) | |
if __name__ == "__main__": | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Final | |
class Point: | |
x: Final[int] | |
y: Final[int] | |
def __init__(self, x: int, y: int) -> None: | |
self.x = x | |
self.y = y | |
def main() -> None: | |
p1 = Point(1, 5) | |
print(p1.x, p1.y) | |
p1.y = 7 | |
print(p1.x, p1.y) | |
if __name__ == "__main__": | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Final | |
class ConnectionBase: | |
address: str = "localhost" | |
timeout: Final[int] = 30 | |
class TCPConnection(ConnectionBase): | |
address: str = "127.0.0.1" | |
# timeout: int = 10 | |
def main() -> None: | |
print(ConnectionBase.timeout) | |
print(TCPConnection.timeout) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment