Basically, I want to take a
Dictionary like { "a":"bar", "b":"blah", "c":"abc", "d":"nada" }
and use it to set variables (in an Object) which have the same name as a key in the dictionary.
class Foo(object)
{
self.a = ""
self.b = ""
self.c = ""
}
So in the the end self.a = "bar", self.b = "blah", etc... (and key "d" is ignored)
Any ideas?
Translating your class
statement to Python,
class Foo(object):
def __init__(self):
self.a = self.b = self.c = ''
def fromdict(self, d):
for k in d:
if hasattr(self, k):
setattr(self, k, d[k])
the fromdict
method seems to have the functionality you request.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With