Skip to content

Instantly share code, notes, and snippets.

@ignertic
Created September 2, 2020 06:35
Show Gist options
  • Save ignertic/9f5538a8f5739f8cae4cd27b689a07be to your computer and use it in GitHub Desktop.
Save ignertic/9f5538a8f5739f8cae4cd27b689a07be to your computer and use it in GitHub Desktop.
class Programmer(object):
def __init__(self):
self.username = None
self.language = None
self.operating_system = None
self.most_used_language=None
self.loves_coffee = None
self.twitter_handle = None
data = {
"username" : "supercode",
"language" : "Python",
"operating_system" : "Ubuntu",
"most_used_language" : "Python",
"loves_coffee" : True,
"twitter_handle" : "iamsupercode"
}
programmer = Programmer()
for key, value in data.items():
if hasattr(programmer, key):
setattr(programmer, key, value)
# the other way of doing it in one line
[setattr(programmer, key, value) for key,value in data.items() if hasattr(programmer, key)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment