'Can I create a Python (3.10) Dictionary with types?
I want to create a Dictionary but also define types of key and values. For example, its C# code would be something like :
Dictionary<string,MyClass> myDict = new Dictionary<string,MyClass>();
is this possible ? If it is not then can I cast the values or keys later in the code?
Solution 1:[1]
Since Python is a dynamically typed language, this is not possible. You can, however, add type hints to e.g. the method returning such a dict to inticate to the user what types to expect.
def make_dict() -> dict[str, MyClass]:
...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Richard Neumann |
