Created
August 7, 2018 06:49
-
-
Save anujkhare/6036cf27bb4116044c7620dc3f59ceaa to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Foo(): | |
def __init__(self): | |
self.foo1 = 1 | |
self.foo2 = 2 | |
self.foo3 = 3 | |
self.foo4 = 4 | |
self.foo5 = 5 | |
self.str_to_attr = { | |
'foo1 string': 'foo1', | |
'foo2 string': 'foo2', | |
'foo3 string': 'foo3', | |
'foo4 string': 'foo4', | |
'foo5 string': 'foo5', | |
} | |
def get_value(self, attr_str) -> None: | |
attr_name = self.str_to_attr.get(attr_str, None) | |
if attr_name is None: | |
raise ValueError | |
val = self.__getattribute__(attr_name) | |
return val | |
def set_value(self, attr_str, val) -> None: | |
attr_name = self.str_to_attr.get(attr_str, None) | |
if attr_name is None: | |
raise ValueError | |
self.__setattr__(attr_name, val) | |
foo = Foo() | |
print(foo.get_value('foo1 string')) | |
foo.set_value('foo2 string', 11) | |
foo.set_value('foo3 string', 12) | |
print('set') | |
print(foo.get_value('foo1 string')) | |
print(foo.get_value('foo2 string')) | |
print(foo.get_value('foo3 string')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment