Class Notes (838,386)
Canada (510,872)
CSC108H1 (113)

december 02 notes

2 Pages
Unlock Document

Computer Science
Michelle Craig

Writing our own classes:  What did we learn last time? o We define a classe (ex. class Event) o We instantiate an object from the class  Ex. e1 = Event()  This calls the constructor, the _ _init_ _ method o An object has a _ _dict_ _ where it keeps track of its instance variables o Just like reguar variables, instance variables are created the first time thyey are assigned a alue o One option is to assign them in _ _init_ _ o Methods are define in the class o And called with dot notation on a specific object from the class  Ex. e1.rename(‘office hourse1.duration() o Extra self parameter refers to calling object  Ex. def rename(self, new_name): # by convention self  e1 becomes self, variable needs to be first one 1. class Event(object): 2. ‘’’Class to represent a single event for my calendar.’’’ 3. 4. def _ _init_ _(self, name, start_time = 9, end_time = -1): 5. ‘’’Create a new Event.’’’ 6. 7. self.start = start_time 8. if end_time < 0: 9. self.end = start_time + 1 10. else: 11. self.end = end_time 12. = name 13. 14. def rename(self, new_name): 15. ‘’’change the name of this een to str new_name.’’’ 16. 17. = new_name 18. 19. def duration(self): 20. ‘’’Return the number of hours 21. 22. return float(self.end - self.start) 23. 24. def make_all_day(self): 25. ‘’’change this eent to run from 9 to 5’’’ 26. 27. self.start = 9 28. self.end = 17 29. 30. def my_str
More Less

Related notes for CSC108H1

Log In


Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.