####Accessing object attributes ##class Student(object): ## pass ## ##s = Student() ##s.first_name = "bob" ##print("s.first_name =", s.first_name) ##print("s.__class__ =",s.__class__) #every object automatically has a class attribute ## ###also possible to add attribute to a class, ###since a class is also an object ##Student.min_hours = 6 #a class variable ##print("Student.min_hours =", Student.min_hours) ##print("s.min_hours =", s.min_hours) # every student instance can access min_hours ####More commonly, we define class attributes within a class ##class Student(object): ## ## #A class variable ## min_courses = 2 #min_courses is the same for all students ## ## #A class method ## def set_course_list(self, courses): ## self.course_list = courses #changes the instance variable ## ## #Another class method ## def enrolled_in(self, course): ## return course in self.course_list ## ## #check if the student meets the minimum number of courses ## def has_min_courses(self): ## return len(self.course_list) >= self.min_courses ## ##s = Student() ##print("s.min_courses =", s.min_courses) ##s.set_course_list( ["i90", "i202", "i206" ] ) ##print("checking if s is enrolled in i90:", s.enrolled_in("i90") ) ##print("checking if s is enrolled in i205:", s.enrolled_in("i205") ) ##print("checking if s has the minimum number of courses:", s.has_min_courses()) ####Demonstrates Constructor and __str__ methods ##class Student(object): ## ## #Constructor method ## def __init__(self, first_name, last_name): ## self.first_name = first_name ## self.last_name = last_name ## ## #Hidden method to control how object is printed ## def __str__(self): ## return "Student: " + self.last_name +", " + self.first_name ## ## ##TA = Student("Zach", "Schneider") ##print(TA.first_name) ##print(TA) ###Refactor the constructor to have default parameters