Getting 2 Instances of the same class in another Class in Python

Question:

So I have the following problem. I got one class (Courses) and another Class (employers). I can already add an instance of the Course Class to an employer, but somehow I can’t add a second instance to the employer class. Everytime I try, I get as a result (<__main__.Course object at 0x0000029B89DF0E10>, <__main__.Course object at 0x0000029B89DF0E50>) Can somebody help me out here? I guess my mistake is somewhere in the __str__-function but I can’t pinpoint where.

Code:

class Course:
    def __init__ (self, title, day, start, end):
        self.title = title
        self.day = day
        self.start = start
        self.end = end

    def __str__ (self):
        res = str(self.title)
        return res

class Employee:
    def __init__(self, firstn, lastn, DoB):
        self.firstn = firstn
        self.lastn = lastn
        self.DoB = DoB
        self.courses = "no courses"

    def add_course (self, course):
        if self.courses == "no courses":
            self.courses = course
        else:
            self.courses = self.courses, course

    def __str__ (self):
        res = str(self.firstn) + " " + str(self.lastn) + ", "
        res += "born " + str(self.DoB) + ", "
        res += "teaches " + str(self.courses)
        return res

if __name__ == "__main__":
    print("Create employee 'John Doe', born January 1st, 1990")
    john = Employee('John', 'Doe', '1990-01-01')
    print('  Created:', john)
    
    print("Create employee 'Jane Doe', born August 10, 1992")
    jane = Employee('Jane', 'Doe', '1992-08-10')
    print('  Created:', jane)
    
    print("Add 'Python I' to John Doe's courses")
    python1 = Course('Python I', 'Tuesday', 12, 14)
    john.add_course(python1)
    print('  John:', john)
    
    print("Add 'Introduction to Semantics' to John Doe's courses")
    semantics = Course('Introduction to Semantics', 'Monday', 10, 12)
    john.add_course(semantics)
    print('  John:', john)
    
    print("Add 'Python I' to Jane Doe's courses")
    jane.add_course(python1)
    print('  Jane:', jane)

Output:

Create employee 'John Doe', born January 1st, 1990

  Created: John Doe, born 1990-01-01, teaches no courses

Create employee 'Jane Doe', born August 10, 1992

  Created: Jane Doe, born 1992-08-10, teaches no courses

Add 'Python I' to John Doe's courses

  John: John Doe, born 1990-01-01, teaches Python I

Add 'Introduction to Semantics' to John Doe's courses

  John: John Doe, born 1990-01-01, teaches (<__main__.Course object at 0x0000029B89DF0E10>, <__main__.Course object at 0x0000029B89DF0E50>)

Add 'Python I' to Jane Doe's courses

  Jane: Jane Doe, born 1992-08-10, teaches Python I

I tried working with different way to write __str__ but it just didn’t work. It shows "Python 1" if just one course i assigned, but if I assign a second course it gives me.

John: John Doe, born 1990-01-01, teaches (<__main__.Course object at 0x0000029B89DF0E10>, <__main__.Course object at 0x0000029B89DF0E50>) 

but it should give me:

John: John Doe, born 1990-01-01, teaches Python I, Introduction to Semantics
Asked By: user11579493

||

Answers:

Use a list (start with an empty one), and do the string formatting in the __str__ method.

class Employee:
    def __init__(self, firstn, lastn, DoB):
        self.firstn = firstn
        self.lastn = lastn
        self.DoB = DoB
        self.courses = []

    def add_course (self, course):
        self.courses.append(course)

    def __str__ (self):
        res = str(self.firstn) + " " + str(self.lastn) + ", "
        res += "born " + str(self.DoB) + ", "        
        courses = "no courses"  # default
        if self.courses:
            courses = ", ".join(str(course) for course in self.courses)
        res += "teaches " + courses
        return res

You can also simplify the __str__ method for Course:

class Course:
.
.
.
    def __str__ (self):
        return self.title

(This assumes title is a string, which makes sense, and is always the case in your examples.)

Answered By: 9769953

If you have a list of objects, then the objects should implement __repr__() because list calls repr() on each element:

    def __repr__ (self):
        return str(self.title)
Answered By: quamrana
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.