help:python:data_class
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
help:python:data_class [2020/08/11 14:48] – [Data Class in Python] gerhard | help:python:data_class [2020/08/11 15:04] (current) – [Data Class in Python] gerhard | ||
---|---|---|---|
Line 22: | Line 22: | ||
- | Also note the `__iter__` function is the classes and the `yield` instead of `return` that allows iteration in the `__iter__` function without the need for a `__next__` function | + | Also note the '' |
<code python> | <code python> | ||
Line 55: | Line 55: | ||
</ | </ | ||
+ | |||
+ | The ' | ||
+ | |||
+ | Data is then type casted to a dictionary that yields the key/value pairs in the for loop and prints them to the console. | ||
<code json> | <code json> | ||
Line 64: | Line 68: | ||
</ | </ | ||
+ | <WRAP center round tip 60%> | ||
+ | The really cool feature of this approach is the ability to keep on dynamically adding variables and getting the iterator to expose them dynamically without knowing what is in the class when passed between functions or even applications. | ||
+ | |||
+ | This approach also sets up the class for passing on json strings between applications with very little code required to prepare the string. | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code python> | ||
+ | |||
+ | import json | ||
+ | |||
+ | json.dumps(dict(data), | ||
+ | |||
+ | </ | ||
+ | |||
+ | The resulting output is: | ||
+ | |||
+ | <code json> | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | }, | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | }, | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | } | ||
+ | </ | ||
help/python/data_class.txt · Last modified: 2020/08/11 15:04 by gerhard