'''
class python_object:
def __init__(self, id, username, password):
self.id = id
self.username = username
self.password = password
#object_dict is made by importing JSON objects and looks like this
object_dict = {1: <__main__.python_object object at 0x00000283EBD7CEC8>,
2: <__main__.python_object object at 0x00000283EBD7CF08>,
3: <__main__.python_object object at 0x00000283EBD7CF48>,
4: <__main__.python_object object at 0x00000283EBD7CF88>}
#///some other code///
id = "10021"
attribute_to_find = input("Enter an Attribute: ")
for i in range(1, len(object_dict)+1):
if (object_dict.get(i)).id == id_to_find:
print(object_dict.get(i).attribute_to_find)
'''
This isn't working because Python is looking for an attribute of 'python_object' called 'attribute_to_find', that much I've figured.
I want the user to be able to enter an attribute and have the program print out the value of that attribute in the class instance with the id "10021" ('id' being another attribute of 'python_object')
Does anyone know how I might go about doing this?
Thanks :)
Use getattr()
, passing the object as the first parameter, and the string name of the attribute as the second parameter as follows:
print(getattr(object_dict.get(i), attribute_to_find))