listpython-3.xclassclass-instance-variables

Python: Can't search through list of class instance variables


I'm trying to create a chemistry GUI that shows various information about each element. I'm using a list of class instances to print out the information, but I continue to get a 'list' object has no attribute 'atomic_number'. This is the class I have set up, along with the code that is giving me errors.

class ElementInformation(object):
    def __init__(self, atomic_number, element_name, element_symbol, atomic_weight, melting_point, boiling_point)
        self.atomic_number = atomic_number
        self.element_name = element_name
        self.element_symbol = element_symbol
        self.atomic_weight = atomic_weight
        self.melting_point = melting_point
        self.boiling_point = boiling_point

def find_element():
    update_status_label(element_information, text_entry)  
    # text entry is a text entry field in TKinter
    # other code in here as well (not part of my question


def update_status_label(element_instances, text_input):

    for text_box in element_instances.atomic_number:
        if text_input not in text_box:
            # do stuff
        else:
            pass

element_result_list = [*results parsed from webpage here*]
row_index = 0
while row_index < len(element_result_list):
    element_instances.append(ElementInformation(atomic_number, element_name, element_symbol, atomic_weight, melting_point, boiling_point))
    # the above information is changed to provide me the correct information, it is just dummy code here
    row_index += 1

my problem is in the function update_status label, specifically the for loop. Python is throwing an error (like i said earlier) that says 'list' object has no attribute 'atomic_number'. For the life of me I can't seem to figure out what's wrong. Thanks for any help!

If it makes any difference, I am using Python 3.x on Windows


Solution

  • Try this instead:

    for element in element_instances:
        text_box = element.atomic_number:
        if text_input not in text_box:
            # do stuff
        else:
            pass
    

    The list "element_instances" is a Python list. It does not have the attribute ".atomic number" even though all elements inside it have such an attribute. Python's for statement assigns each element of the list to a variable - that element is an instance of your custom class, where you can fiddle with your attributes.