Skip to content Skip to sidebar Skip to footer

How To Reduce Boilerplate When Initializating Classes From Jsons In Python 3.5?

I have a set of python webservices that work with data objects they get via a JSON POSTs. In my old services I have a lot of boilerplate to (de)serialize and check JSONs for each o

Solution 1:

No, PEP 484 and type hints in general will not help you reduce the boilerplate.

The simplest explanation is that python run-time is not aware of PEP 484. Python interpreter executes type hints only to make sure the syntax is correct, there are no undefined names, etc.

Therefore, there is no way to meaningfully change the run-time behavior of your program by using type hints.

Type hints are only processed in a separate phase, before you execute the interpreter, to verify that your types are correct. No information flows from that analysis to the python interpreter.

Of course you can read type annotations for your source code in run-time yourself, and then do what you want with them. But that's clearly not what you're asking, since in order to do anything useful with that approach, you'd have to write a lot of rather complex code.

Post a Comment for "How To Reduce Boilerplate When Initializating Classes From Jsons In Python 3.5?"