Created
September 2, 2020 06:35
-
-
Save ignertic/9f5538a8f5739f8cae4cd27b689a07be to your computer and use it in GitHub Desktop.
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 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