A class defines an object’s data and behavior, while __init__ establishes its initial state.
Basic Class Structure
class ClassName:
def __init__(self, parameters):
self.attribute = value
def method(self):
pass
class Zombie:
def __init__(self, name, health):
self.name = name
self.health = health
def groan(self):
print(f"{self.name}: Graaah! ๐ง")
walker = Zombie("Walker", 50)
walker.groan() # Walker: Graaah! ๐ง
__init__: The Constructor
__init__ is called automatically when an object is created. Analogy: when a baby is born, it’s immediately given a name and a date of birth โ __init__ does the same thing for objects.
class Human:
def __init__(self, name, health, age):
self.name = name
self.health = health
self.age = age
self.kills = 0 # you can set a default value here
rick = Human("Rick", 100, 35)
print(rick.name) # Rick
print(rick.kills) # 0 (even though we didn't pass it!)
When you write Human("Rick", 100, 35), Python automatically calls __init__(self, "Rick", 100, 35).
self: A Reference to the Object
self is a reference to the specific object. When you write self.name, you’re saying: “the name attribute of this object.”
Without self it doesn’t work:
class Zombie:
def __init__(self, name):
name = name # โ local variable, dies after __init__
walker = Zombie("Walker")
print(walker.name) # AttributeError!
With self it works:
class Zombie:
def __init__(self, name):
self.name = name # โ
object attribute, persists
walker = Zombie("Walker")
print(walker.name) # Walker
Each object stores its own attributes:
walker = Zombie("Walker", 50)
runner = Zombie("Runner", 30)
print(walker.name) # Walker
print(runner.name) # Runner โ its own attribute!
print(walker.health) # 50
print(runner.health) # 30
self in Methods
class Zombie:
def __init__(self, name, health):
self.name = name
self.health = health
def groan(self):
print(f"{self.name}: Graaah!") # self.name โ object attribute
def take_damage(self, damage):
self.health -= damage # modifying the attribute
print(f"{self.name} took {damage} damage. Health: {self.health}")
def attack(self):
self.groan() # calling another method via self
print(f"{self.name} attacks!")
walker = Zombie("Walker", 50)
walker.take_damage(20) # Walker took 20 damage. Health: 30
walker.attack() # Walker: Graaah! / Walker attacks!
Default Parameters
class Zombie:
def __init__(self, name, health=50, speed=2):
self.name = name
self.health = health
self.speed = speed
walker = Zombie("Walker") # health=50, speed=2 (defaults)
runner = Zombie("Runner", 30, 5) # health=30, speed=5
print(walker.health) # 50
print(runner.speed) # 5
Parameters with defaults must come after required parameters.
Common Mistakes
Mistake 1: Forgot self. โ the attribute won’t be saved
def __init__(self, name):
name = name # โ โ should be: self.name = name โ
Mistake 2: Forgot self in method parameters
def __init__(name): # โ no self โ TypeError
def __init__(self, name): # โ
Mistake 3: Typo in __init__
def _init_(self, name): # โ one underscore โ won't work
def __init__(self, name): # โ
two underscores on each side
Mistake 4: Accessing an attribute without self inside a method
def groan(self):
print(f"{name}: Graaah!") # โ NameError
print(f"{self.name}: Graaah!") # โ
Summary
__init__ โ the constructor, called automatically when you write ClassName(arguments).
self โ a reference to the object; it lets you:
- save attributes: self.name = name
- read attributes in methods: print(self.name)
- call other methods: self.other_method()
class ClassName:
def __init__(self, parameters):
self.attribute = value
obj = ClassName(arguments)
๐ฌ Comments (0)
No comments yet
Be the first to share your opinion about this article!