Python Fundamentals / Functions and errors
Lesson 37 of 38
Classes and objects
A class is a blueprint for objects that bundle data and functions together. __init__ sets up each new object, and self refers to that object.
Key points
- class Name:
- __init__ runs when you create one
- Methods take self first
class Player:
def __init__(self, name):
self.name = name
self.hp = 100
def hit(self, dmg):
self.hp -= dmg
p = Player("Lazo")
p.hit(30)
print(p.hp) # 70Quiz · +10 XP
What does self refer to inside a method?
Log in to save progress and earn XP.