Last active
December 22, 2015 02:59
-
-
Save bodiam/6407555 to your computer and use it in GitHub Desktop.
Transforming a dict to objects in Python
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
class Light: | |
def __init__(self, id, name): | |
self.id = id | |
self.name = name | |
response = {"1": {"name": "bedroom"}, "2": {"name": "kitchen"}} | |
lights = [Light(id, response[id]["name"]) for id in response] | |
for x in lights: | |
print('{} : {}'.format(x.id, x.name)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'd avoid use of the % operator and user the format method instead.