'Python: How do I add static variables to a class dynamically without an object
Love the community; very helpful always, so thanks. On to my question...
I am using FastAPI. I considered asking Tiangolo about this directly but I think it is a Python question TBH so I'm asking here first.
Currently, I have a file called countries.py as follows:
from enum import Enum
class Countries(str, Enum):
AF = "AF"
AX = "AX"
AL = "AL"
DZ = "DZ"
AS = "AS"
In my model file I have this code:
from pydantic import BaseModel
from .countries import Countries
class SomeModel(BaseModel):
countryInQuestion: Countries
Now what I really want to be able to do is something like setattr (which is only available on objects) but on the static class to define the countries.
Something along the lines of:
from country_list import countries_for_language
countries = dict(countries_for_language('en'))
and in the model I would want to somehow apply the dict as follows:
{k:k for k in countries.keys()}
Any help would be most welcome
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
