Adding methods and passing objects
20-minute lesson · module-classes-03
You are previewing this course as a guest
Read anything and run any exercise you like. It all runs in your own browser, so nothing here is saved: no XP, no progress, and the AI tutor needs an account. Sign in and your work starts counting.
Adding methods and passing objects
20-minute lesson · module-classes-03
- Define a method inside a class and call it on an instance.
- Use `self` inside a method to read and update the instance's attributes.
- Write a method that takes another object as an argument.
- Distinguish a method (called on an object) from a plain function.
Source attribution
Status: adapted
How to Think Like a Computer Scientist: Interactive Edition — source · Classes and Objects - the Basics / Adding Other Methods / Objects as Arguments and Parameters
Adaptation notes. Reading rewritten in our voice. Methods are added to a Student class (enroll, is_full_time) and a method takes another Student to compare credit loads. Hidden tests call methods and pass objects as arguments.
License notes. Original chapter is GNU FDL 1.3 (Brad Miller and David Ranum). No verbatim text from the original is reproduced here.