0

I am really new to Python but learning quickly. I know what I want to accomplish, just not sure how to phrase the question or terminology.

I'm wanting to add a unique student to each classroom, if I add an existing student name, I expect the add_student function to not add the student to the class.

class Student:
    def __init__(self, student_name):
        self.name: str = student_name
        self.age: int = 0
        self.gender: str = ''

class Classroom:
    def __init__(self, classroom_name):
        self.classroom_name: str = classroom_name
        self.teacher_name: str = ''
        self.student_list: Student = []

    def add_student(self, student_name):
        if student_name not in self.student_list:
            self.student_list.append(student_name)

    def remove_student(self, student_name):
        if student_name in self.student_list:
            self.student_list.remove(student_name)

    def list_student(self):
        for s in self.student_list:
            print(s.name)

classroom = Classroom('Science')

std1 = Student('Joe')
std2 = Student('Joe')
std3 = Student('Joe')
classroom.add_student(std1)
classroom.add_student(std2)
classroom.add_student(std3)

classroom.list_student()
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Andrew H.
  • 13
  • 2
  • A comment to the logic. I think having student object carry the class information might be better. This why a student can't be in a class twice. – MSH Aug 24 '21 at 21:08

3 Answers3

0

Remember that you're not adding the student's name, you're adding a student OBJECT.

    def add_student(self, student):
        if not any( [s.name == student.name for s in self.student_list] ):
            self.student_list.append(student)
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • Ok, so i guess I'm not missing any "key" attribute or some parameter that can assign an ID to an object record. I think that was what had expected to have to do, but this turns out the same, just totally different than what I was expecting. Is that called list comprehension? – Andrew H. Aug 24 '21 at 20:58
  • Depends on what you want. If you require the name to be unique, then you're nearly there. If you want to allow multiple "Joe" students, then you'll need another primary key. – Tim Roberts Aug 24 '21 at 21:00
0

In real life, classrooms can have multiple students with the same name, so your code actually works properly already, i.e. it won't add the same Student object again.

>>> classroom.student_list.count(std1)
1
>>> classroom.add_student(std1)
>>> classroom.student_list.count(std1)
1

The only thing you should change is the student_name parameter to student.

Keep in mind that Student objects should correlate with real-life students. If there are multiple objects for one person, that's a problem.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • The code I posted did not work, it added 3 instances of 'Joe' to the classroom object when it should have only added 1. The code sample Tim had provided works and only allows a single addition. Thanks for your input! – Andrew H. Aug 24 '21 at 21:04
  • @Andrew My point is that you're confused about how objects should work. See the last paragraph I just edited in. – wjandrea Aug 24 '21 at 21:05
  • I may not know how Python classes work to the fullest which is why im here. I know my example may have been WAY over simplified but at face value i thought I did a good job trying to put together an example regardless of whether there may be many "Joe"s in class or not. I could have made it License Plates in State or Serial Numbers in Product. Overall, I'm just trying to discover the best way to ensure some type of uniqueness. Thats all. – Andrew H. Aug 24 '21 at 21:12
  • @Andrew The same thing applies in those cases too: if you have multiple objects for one license plate or one serial number, that's a problem. Objects are supposed to be unique, which is why for example, `std1 is std2` -> `False`. – wjandrea Aug 24 '21 at 21:41
0

First let's understand that, you think when your student object has same name, the objects are the same. That is not the case.

Your student objects has ids and when the ids match, the objects match. So can you change it? Yes. See: https://stackoverflow.com/a/14535739/2681662

Now since you can determine what is the "same student" you should use a set as student_list since Set items are unordered, unchangeable, and do not allow duplicate values..

MSH
  • 1,743
  • 2
  • 14
  • 22