Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Summary

In this lesson, you learned that:

  • A class says what every object of one kind has and can do: class Pet:. Class names are written in CapWords.
  • An object is created by calling its class like a function: rex = Pet("Rex", "dog", 3). One class can make as many objects as you need, and each has its own values.
  • __init__ runs by itself when an object is created. It stores values in the object's attributes, from its parameters (self.name = name) or as a starting value (self.hunger = 5). A missing argument ends with a TypeError.
  • You read and change an attribute after a dot: rex.name, rex.age += 1. An attribute that the object doesn't have ends with an AttributeError.
  • A method is a function in a class, def describe(self):, and you call it on an object: rex.describe(). Its first parameter, self, is the object it was called on. Python passes it by itself, so a method without self ends with a TypeError. Inside a method, an attribute is self.name, not name.
  • A method can return a value worked out from the attributes, or change them, and keep them sensible, such as a hunger that never drops below 0.
  • __str__ returns the text that print() and f-strings show for an object. Without it, print(rex) shows <Pet object at 0x…>.
  • A list can hold objects. A loop calls a method on each of them, and max(pets, key=by_age) returns the object with the largest key.
  • Inheritance: class Dog(Pet): is a child class that gets everything from its parent class. It can add methods and replace the parent's. When it has an __init__ of its own, that calls the parent's with super().__init__(…).

In the next lesson, the Final Project, you'll put together what you've learned in this course and build the receipt for an online grocery order step by step, with every item in the cart as an object.

Instructions

  1. 1

    Let's practice with the book from the lesson on dictionaries, this time as an object. In place of the comment, write a class Book. Its __init__ takes title and pages and stores them as attributes, and it also creates an attribute page, the page you're on, which starts at 0. Add __str__, which returns The Hobbit: page 0 of 310. Keep the line hobbit = Book("The Hobbit", 310) as it is.

  2. 2

    Add a method read(self, count) that moves page forward by count pages, but never past the last page. At the end of the program, call hobbit.read(100) and print the book, then hobbit.read(250) and print it again. The last two lines should be The Hobbit: page 100 of 310 and The Hobbit: page 310 of 310.

Back

11/11

  1. 1. A Class and Its Objects
  2. 2. Attributes
  3. 3. Methods
  4. 4. Methods That Change the Object
  5. 5. Quiz: Objects
  6. 6. Printing an Object
  7. 7. A List of Objects
  8. 8. Inheritance
  9. 9. Quiz: Inheritance
  10. 10. super()
  11. 11. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.