How to access an object in an array python

So, the fundamental problem is exactly what the error message is implying:

AttributeError: 'function' object has no attribute 'attribute1' 

And that is becauseitems[0].attribute1 is trying to access attribute on a function object, becauseitems[0] is a function object. Note:

one_thing = Thing()
for i in range(2):
    list_of_things.get_thing(one_thing.give_a_thing)

Realize that one_thing.give_a_thing returns the method itself, you want to call the method:

one_thing = Thing()
for i in range(2):
    list_of_things.get_thing(one_thing.give_a_thing())

Aside from that, this code is incredibly strangely structured. Why is give_a_thing simply returning the object itself? That means your list_of_things is simply going to be a list containing multiple references to the same object.

You probably want something like

class Thing:
    def __init__(self, attribute1='y', attribute2='n'):
        self.attribute1 = attribute1
        self.attribute2 = attribute2


class ThingOfThings:
    def __init__(self, items=None):
        if items is None: # watch out for the mutable default argument
            items = []
        self.items = items
    def add_thing(self, thing): # use a better name
        self.items.append(thing) # don't create a needless intermediate, single-element list

Then simply:

list_of_things = ThingOfThings()

for _ in range(2): # style tip: use _ if iterator variable is not used
    list_of_things.add_thing(Thing()) # create *new* Thing each iteration

print(list_of_things.items[0].attribute1)

Home » Python » Python programs

Here, we are going to learn about the array of objects in Python and going to explain it by writing Python program to take student information and store it into an array of objects and then print the result.
Submitted by Shivang Yadav, on February 15, 2021

Python objects are the instances of class in Python. And storing multiple objects into an array is an array of objects.

Problem Statement: We need to store details of multiple students in an array of objects. And then print the students' results.

Problem Description: We will take the students' details, (roll number, name, marks in physics, chemistry and maths) from users for multiple students as required by users. And the print the result that displays student's roll number, name and percentage ( sum of all marks  / 300 * 100).

Algorithm:

  • Step 1: Create a class named Student to store student information.
  • Step 2: Take inputs from the user, and store it into an array of objects using getStudentInfo() method.
  • Step 3: After the user has entered all student's information. Print the result.
  • Step 4: The result printed as roll number, name, and percentage using printResult() method.

Program to illustrate arrays of Objects in Python

class Student:
    def GetStudentInfo(self):
        self.__rollno = input("Enter Roll Number ")
        self.__name = input("Enter Name ")
        self.__physics = int(input("Enter Physics Marks "))
        self.__chemistry = int(input("Enter Chemistry Marks "))
        self.__maths = int(input("Enter Math Marks "))
    def printResult(self):
        print(self.__rollno,self.__name, ((int)( (self.__physics+self.__chemistry+self.__maths)/300*100 )))

StudentArray = []

while(True):
    student = Student()
    student.GetStudentInfo()
    StudentArray.append(student)
    ch = input("Add More y/n?")
    if(ch=='n'):break

print("Results : ")

for student in StudentArray:
    student.printResult()

Output:

Enter Roll Number 001
Enter Name John
Enter Physics Marks 87
Enter Chemistry Marks 67
Enter Math Marks 90
Add More y/n?y
Enter Roll Number 002
Enter Name Jane
Enter Physics Marks 54
Enter Chemistry Marks 87
Enter Math Marks 98
Add More y/n?n
Results : 
001 John 81
002 Jane 79

Python class & object programs »



How do you access an element in an array of objects in Python?

Just like lists, we can access elements of an array by indexing, slicing and looping..
Indexing Array. ... .
Slicing Array. ... .
Looping Array. ... .
Using insert() Method. ... .
Using append() method. ... .
Using and Slicing. ... .
Using extend() method. ... .
Using fromlist() Method..

How do you access things in an array?

An item in a JavaScript array is accessed by referring to the index number of the item in square brackets. We know 0 will always output the first item in an array. We can also find the last item in an array by performing an operation on the length property and applying that as the new index number.

How do you use an array of objects in Python?

Arrays of Objects Example in Python.
Step 1: Create a class named Student to store student information..
Step 2: Take inputs from the user, and store it into an array of objects using getStudentInfo() method..
Step 3: After the user has entered all student's information..