Home > Software engineering >  Editable Callers for Class Attributes - Python
Editable Callers for Class Attributes - Python

Time:12-15

I am working on a mock Student Database using OOP in python, and I am trying to use a function to search for certain parameters of a Class.

In the following example, School is a large class that holds instances of Students as one of its arguments. Hence (For Student in School.Student)

found_list = []

class Student():
    def __init__(self, name, age, gender, name_of_school, class_type):
        self.name = name
        self.age = age
        self.gender = gender
        self.name_of_school = name_of_school
        self.class_type = "S"

Example_Student = Student("Joseph", 8, "male", "The School", "S")

gender_to_be_found = input("Enter the gender to be searched for  ")

for Student in School.Student:
    if Student.gender == gender_to_be_found:
        found_list.append(Student)

This works as a principle but I am wanting to do it in the below format, so I can search for various attributes of a Class through one function

def search_for(value_to_be_found, values_to_searched_from, end_list, class_argument):

 if end_list != values_to_searched_from:
     for possible_targets in value_to_be_found:
         for possible_matches in values_to_searched_from:
             try:
                 if possible_targets == possible_matches.class_argument:
                     end_list.append(possible_matches)
             except:
                 pass
 else:
     for possible_targets in value_to_be_found:
         for possible_matches in values_to_searched_from:
             try:
                 if possible_targets != possible_matches.class_argument:
                     end_list.remove(possible_matches)
                      
             except:
                 pass        

so that I can pass in the (class_argument) "gender"

and automatically search for any Student.gender that matches my value_to_be_found

search_for("Joseph", School.Student, found_list, "name")

Clearly this proposed method (above) is non-functional, but I feel like there is a way to do this that I have not managed to quite achieve. This error is produced:

AttributeError:  object has no attribute 'class_argument'

Thanks in advance for any help :)

CodePudding user response:

You could add a search function to School, using getattr to access attributes of an object:

class Student():
    def __init__(self, name, age, gender, name_of_school, class_type):
        self.name = name
        self.age = age
        self.gender = gender
        self.name_of_school = name_of_school
        self.class_type = "S"


class School:
    def __init__(self):
        self.students = []

    def add(self, student):
        self.students.append(student)

    def search(self, value, search_attribute):
        result = []
        for s in self.students:
            student_value = getattr(s, search_attribute, None)
            if student_value == value:
                result.append(s)
        return result


s1 = Student("Joseph", 8, "male", "The School", "S")
s2 = Student("Joseph2", 9, "male", "The School", "S")
s3 = Student("Joseph3", 10, "male", "The School", "S")

s = School()
s.add(s1)
s.add(s2)
s.add(s3)

print(s.search("Joseph", "name"))
print(s.search(10, "age"))
print(s.search("gender", "binary"))
print(s.search("The School", "name_of_school"))

Out:

[<__main__.Student object at 0x107c19fd0>]
[<__main__.Student object at 0x107c19ee0>]
[]
[<__main__.Student object at 0x10ab16fd0>, <__main__.Student object at 0x10ab16fa0>, <__main__.Student object at 0x10ab16ee0>]
  • Related