Skip to content

Methods, self and __str__

Write methods that read and change an object's attributes through self, and give the class a __str__ that returns the text print shows.

It opens on

Ada's account holds 100 and Ben's holds 50. After a.deposit(30) and then b.deposit(5), what does b.balance show: 55, 85 or 135?

Why it matters

A class is useful because its objects can do things, such as an account that deposits and prints itself, and the standard slips here (a missing self, a missing self., a __str__ that prints instead of returning) are the commonest errors in a first object-oriented program.

Good to know first

  • Classes, objects and __init__: self.balance = balance stores an attribute in the object
  • Defining and calling functions: a call passes arguments to parameters in order
  • Defining and calling functions: a function with no return gives None

Step by step

  1. A method works on its own object

    Write a method that changes an attribute through self, and say which object a call such as a.deposit(30) changes.

    Lines 7 and 8 make Ada's object with balance 100 and Ben's with 50.

    two names, two objects

    A common mix-up

    A method belongs to one particular object, or changes every account.

    In fact: The method is shared code, and self is whichever object stands before the dot, so only that object changes.

  2. The two slips with self

    Spot a method that lacks self in its parameters and one that lacks self. on an attribute, and say what each does when run.

    The run stopped with TypeError, BankAccount.deposit() takes 1 positional argument but 2 were given. The count includes the self that Python added, though the call shows only the 30.

    the traceback

    A common mix-up

    Inside a method, balance and self.balance are the same thing.

    In fact: The plain name is a local variable lost at the return, and only self.balance is stored in the object.

  3. print and __str__

    Say why print(a) on a plain object shows an address, and write a __str__ that returns the text to show.

    Our __str__ takes only self, and it returns an f-string built from the owner and the balance. Python calls it whenever the object is printed or turned into text with str().

    the class with __str__ returning an f-string

    A common mix-up

    __str__ should print the account.

    In fact: It must return a string; print(a) does the printing, and a __str__ that prints returns None.

Try it

With c = BankAccount("Cy", 10), then c.deposit(5) and c.deposit(5), what does c.balance show?Answer

20

Hint: Each call reads what the last one stored

A method written def reset(): and containing self.balance = 0 is called as a.reset(). What error, and what is the fix?Answer

TypeError, since two values arrive for no parameters; write def reset(self):

Hint: Count what arrives

For a __str__ that returns self.owner + " owns " + str(self.balance) with owner Cy and balance 5, what does print(c) show?Answer

Cy owns 5

Hint: Return the text, then print shows it

Line 9 is a.deposit(30). Inside deposit, what does self.balance on the right of the equals sign read, and where does the new value go?Answer

It reads Ada's current balance, 100, and the sum 130 is stored back in Ada's object.

The method has only the one parameter, amount. What kind of error does a.deposit(30) give?Answer

a TypeError, since two values arrive and the method accepts one.

Methods, self and __str__

30 minutes every month. No card.