Classes, objects and __init__
Write a class with __init__ and attributes, make objects from it, and say that each object keeps its own attributes while two names can point to one object.
It opens on
Two accounts come from one class, Ada's with 100 and Ben's with 50. After a.balance = 120, what is b.balance: 100, 120 or 50?
Why it matters
Almost every program past the first weeks is built from classes (an account, a player, a file handle), and the commonest first bugs are a forgotten self., a missing argument, and an object changed through a second name; one picture of names pointing at boxes explains all of them.
Good to know first
- Defining and calling functions: a call matches arguments to parameters in order
- Aliasing and mutability: b = a makes a second name for the same object
- Default and keyword arguments and docstrings: a default is used when the argument is left out
Step by step
A class and its objects
Read a class with __init__, say what each line of a call such as BankAccount("Ada", 100) does, and draw the object it makes.
Line 6 makes one object. Inside __init__, self is that new object, owner is "Ada" and balance is 100, so the object ends up with owner Ada and balance 100, and the name a points to it. Line 7 does the same for Ben, which makes a second object with its own attributes and the name b pointing at it.
two names pointing at two objects
A common mix-up
Changing a.balance changes every BankAccount.
In fact: The class is only the plan; each instance has its own balance, and a.balance reaches only the object a points to.
Two names, one object
Draw c = a as a second arrow into the same object, and tell is from the creation of a new object.
Lines 1 and 2 make the two objects as before. Line 3 only copies the arrow, so c points at the object a points to. No new object is made and __init__ does not run, which is aliasing, as with two names for one list.
three names, two objects
A common mix-up
c = a makes a second account with the same balance.
In fact: It makes a second name for the one account, so money taken out through c is gone from a.
Mistakes in __init__
Spot the missing self. in __init__ and the missing argument in a call, read the errors they give, and fix them.
The run printed Ada, and then line 8 stopped the program with an AttributeError saying the BankAccount object has no attribute balance. The object never received a balance, so the dot found nothing to read.
what Python printed
A common mix-up
Writing balance = balance in __init__ saves the balance in the object.
In fact: Without self. it is only a local name, and the object keeps nothing.
Try it
After p = BankAccount("Dee", 5) and p.balance = p.balance + 10, what does print(p.owner, p.balance) show?Answer
Dee 15
Hint: The attribute is read, then written back
With x = BankAccount("Eli", 40), y = x and y.balance = 5, what does print(x.balance) show?Answer
5
Hint: y is another name for x's object
In __init__(self, name, age) the lines are self.name = name and age = age. After p = Pet("Rex", 3), which of p.name and p.age gives an error?Answer
p.age
Hint: Only the line with self. stores anything
When line 7 runs BankAccount("Ben", 50), which object is self inside __init__, and how many times has __init__ run in all?Answer
Self is Ben's new object, the second one; __init__ has run twice, once for each object made.
Line 4 is c.balance = 0. What does print(a.balance) show?Answer
0, since a and c are two names for one object.
