Class 属性的可编辑呼叫者 - Python

Editable Callers for Class Attributes - Python

我正在 python 中使用 OOP 开发一个模拟学生数据库,我正在尝试使用一个函数来搜索 Class.

的某些参数

在下面的示例中,School 是一个大型 class,它将 Students 的实例作为其参数之一。因此(对于 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)

这是一个原则,但我想用下面的格式来做,所以我可以通过一个函数

搜索Class的各种属性
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        

这样我就可以传入(class_argument)“性别”

并自动搜索与我的 value_to_be_found

匹配的任何 Student.gender
search_for("Joseph", School.Student, found_list, "name")

很明显,这个建议的方法(上文)没有用,但我觉得有一种方法可以做到这一点,但我还没有完全实现。 产生此错误:

AttributeError:  object has no attribute 'class_argument'

提前感谢您的帮助:)

您可以向 School 添加搜索功能,使用 getattr 访问对象的属性:

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"))

输出:

[<__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>]