Python Forum
Class and calling a method
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class and calling a method
#2
You need to construct the string in the __str__ method if that is what you want it to look like.
Also you need to decide if Student.course is a list or a single item.

class Student(object):
    def __init__(self, first_name, last_name, student_id, year):
        self.first_name = first_name
        self.last_name = last_name
        self.student_id = student_id
        self.year = year
        self.course = []
         
    def __str__(self):
        return "{} is enrolled in {}.".format(self.first_name, self.course)
         
    def enroll(self, course):
        self.course.append(course)
     
 
def main():
    student = Student('John', 'Smith', 'L01234567', 'senior')
    student.enroll('CSC225')
    print(student)
     

main()
Reply


Messages In This Thread
Class and calling a method - by Zatoichi - Feb-24-2018, 04:32 AM
RE: Class and calling a method - by Mekire - Feb-25-2018, 03:40 AM
RE: Class and calling a method - by piday - Feb-26-2018, 08:26 PM
RE: Class and calling a method - by Zatoichi - Mar-13-2018, 08:44 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Please, how do I call the method inside this class Emekadavid 1 1,676 Jun-26-2020, 01:26 PM
Last Post: Yoriz
  class method on two object gabejohnsonny21 4 2,469 Apr-22-2020, 06:57 AM
Last Post: DeaD_EyE
  Calling an class attribute via a separate attribute in input wiggles 7 2,926 Apr-04-2020, 10:54 PM
Last Post: wiggles
  Class/Method Confusion ramadan125 1 2,618 Sep-10-2018, 12:19 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020