Until now you have used the kinds of thing that Python gives you. A class lets you make your own: a bank account with a balance and a deposit action, a student with marks and an average, a book that can be borrowed and returned. In this course you see that you already use objects every day, write a class with its attributes and methods, keep the rules of a thing inside the thing, make many objects from one class, and tell the data of the class from the data of one object. A small library system is the project at the end. The practice runs your classes on real cases and tells you which ones fail.
5 lessons, about 2 h 30 min5 simulations9 problems10 practice questionsPython in every lesson
Every value in Python is an object: it has data, called attributes, and actions, called methods, and you reach both with a dot. A string, a list and even a number are objects of a kind, called a class, and type tells the class of a value. A method is called with brackets and an attribute is read without them. dir lists what an object can do. A class is a kind of thing, and an object is one example of it. Knowing this makes the idea of writing your own class natural.
A class is written with the word class, a name that starts with a capital letter, and a block of methods. The method __init__ runs when an object is made and sets up its attributes, and the first parameter of every method is self, the object itself. Attributes are made by writing self dot name equals value, and they belong to one object. You make an object by calling the class like a function, and each object has its own data. A method without self, or an assignment without self dot, are the two usual mistakes.
An object has a state, which is the values of its attributes, and its methods read and change that state. A method can return an answer or change the data, and a good method does one of the two clearly. The rules of a thing belong inside the class: a bank account refuses a withdrawal of more money than it has, and refuses a deposit that is not more than zero, by raising a ValueError. A caller then needs no check of its own. Default values for parameters work in methods as in functions. Test a class by making objects and checking the state after each action.
Objects are kept in lists and dictionaries and processed with loops, and max and sorted with a key function that returns an attribute or the answer of a method find the best or sort them. An attribute that belongs to the class, written in the class body, is shared by all the objects, which is right for a species name or a counter, while an attribute set with self belongs to one object. Writing to an object attribute with the same name makes a new attribute on that object only. A variable holds a reference to an object, so two names can point to one object. A default value that is a list is made only once and is shared by every object, so use None and make a new list inside.
In this project we build a library system with two classes. A Book knows its title and author and who has it, and a Library keeps a list of books and a log, and can add a book, find a book by title ignoring capitals, lend a book to a person, take it back, and list the books that are available. The rules live inside the classes: a book that does not exist, a book that is already out and a return of a book that is not out are refused with a ValueError. We plan the classes and a table of expected states, build the program, test the refused actions, and extend the library with due dates and a limit on the books of a person.
Log in to join the discussion.
Loading…