Course 1.1Lesson 4 of 5
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.
Try the code and the simulations beside the lesson.
After this lesson you can
A class is useful because you can make many objects from it. A class of students has many students. A library has many books. You keep them in a list, and use the loops, max and sorted that you know.
Predict first. Which student has the best average? Then run it.
students holds three Student objects. The loop for s in students gives one object in each round, and s.average() calls its method.max(students, key=by_average) finds the object whose average is the biggest. The key function takes one object and returns the thing to compare. This is the same key= that you used to sort words by length.sorted(students, key=by_average, reverse=True) puts the objects in order.[s.name for s in students if s.average() > 70] picks the names of the students above 70.Objects are just values. You can keep them in lists and dictionaries, give them to functions, and return them.
So far each attribute belonged to one object, set with self.. But sometimes a value belongs to the whole class. Every dog belongs to the same species. And we may want to count how many dogs were made. For that, write the attribute in the class body, outside any method.
| Written | Belongs to | Use it for | |
|---|---|---|---|
| class attribute | in the class body | the class, shared | a species, a limit, a counter |
| object attribute | self.x = ... | one object | the name, the balance, the marks |
A variable holds a reference to an object, like a label on it. You met this with lists.
Making a copy needs work. The line Box(list(a.items)) makes a new list and a new object, so c is independent. When an object holds a list, think about whether you share it or copy it.
The cure is the pattern in FixedTeam: use None as the default, and make a new list inside, when the default is used:
self.members = [] if members is None else membersNow each object gets its own list. The rule: never use a list, a dictionary or a set as a default value. Use None.
The default value is worked out once, when def runs, not each time the method is called. That is why a list default is shared.
b = a is a copy.key=Student.average is fine, key=s.average() is not.self. before a class attribute in the class body. Names in the class body have no self.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
max and sorted with a key function.b = a gives the same object a second name.None.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
rex.species = "Wolf", what is Dog.species?members=[] a bad default?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.