Code found in a library I was considering using... NEVER write code like this :)
class Object: | |
attributes = ["myattr", | |
"anotherattr", | |
"attr1", | |
"someattr", | |
"attr5"] | |
code = "def __init__(self" | |
for att in attributes: | |
code = code + ", " + att + " = None" | |
code = code + "):\n" | |
for att in attributes: | |
code = code + " self." + att + "=" + att + "\n" | |
exec(code) | |
def toStr(self): | |
res = "(" | |
for att in Object.attributes: | |
boo = eval("isinstance(self. " + att + ", str)") | |
if not boo: | |
res = res + str(eval("self." + att)) | |
else: | |
res = res + "\"" + str(eval("self." + att)) + "\"" | |
if att != Object.attributes[len(Object.attributes) - 1]: | |
res = res + "," | |
res += ")" | |
return res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment