Classes

INPUT · Slides

Writing what it can do onto the blueprint

01 / 07

Putting the work next to the data

What hurt in the first lesson of this group was that the member data and the functions using it sat in different places.

A class can hold functions too. A function written inside a class is called a method. Now the data and the work are in the same box.

02 / 07

How to write a method

Just as with __init__, you write def inside the class, indented. The first argument is always self.

You call it with a dot on the instance, as in member.greet(). You do not write self yourself — Python fills in who did the calling.

class Member:    def __init__(self, name):        self.name = name    def greet(self):        print(f"Hello, {self.name}")member = Member("Aoi")member.greet()

Result

Hello, Aoi

03 / 07

self is that real one itself

Even with a hundred members made from the same blueprint, self.name is the name of whoever has just been called.

It looks like an incantation at first, but all self does is say whose data we are talking about. With a.greet(), self is a; with b.greet(), self is b.

class Member:    def __init__(self, name):        self.name = name    def greet(self):        print(f"Hello, {self.name}")a = Member("Aoi")b = Member("Hinata")a.greet()b.greet()

Result

Hello, Aoi
Hello, Hinata

04 / 07

Leave self out and

Define it as def greet(): with no self, call member.greet(), and you get an error.

The caller passed nothing, and yet it is told one was given. That is the clearest proof that the real one being called is passed in automatically.

class Member:    def greet():        print("Hello")member = Member()member.greet()

Result

TypeError: Member.greet() takes 0 positional arguments but 1 was given

05 / 07

A method can return a value too

Beyond calling print, you can write a method that hands a value back with return. Exactly the functions of Chapter 3.

Leaving the showing to the caller makes it easier to reuse later, as you know.

class Member:    def __init__(self, years):        self.years = years    def yearly_fee(self):        if self.years >= 3:            return 4000        return 5000member = Member(3)print(member.yearly_fee())

Result

4000

06 / 07

A method can take arguments too

You can line ordinary arguments up behind self. When you call it, you skip the self one and start from the second.

Overwrite self.points inside the method and the state of that real one changes. Something like collecting points then writes itself.

class Member:    def __init__(self, points):        self.points = points    def earn(self, amount):        self.points += amountmember = Member(10)member.earn(5)print(member.points)

Result

15

07 / 07

The data and the work are one

What was scattered in the first lesson — displaying a member, working out the yearly fee — is now gathered inside Member.

Want to rename an item? The only place to look is inside this class. That is the biggest prize a class pays out.

Next lesson we decide how it looks when you hand the whole thing over with print(member).

class Member:    def __init__(self, name, years):        self.name = name        self.years = years    def label(self):        return f"{self.name} (member, year {self.years})"    def yearly_fee(self):        if self.years >= 3:            return 4000        return 5000member = Member("Aoi", 3)print(member.label())print(member.yearly_fee())

Result

Aoi (member, year 3)
4000