Module 5: Introduction to Object-Oriented Programming (OOP)
Attributes and Methods
-
Attributes (Instance Variables): Variables that belong to an object. They store the data or state of the object. Attributes are typically defined within the
__init__
method usingself.attribute_name = value
.class Dog: def __init__(self, name, age): self.name = name # Instance attribute self.age = age # Instance attribute
-
Methods: Functions that belong to a class. They define the behavior or actions that an object can perform. Methods always have
self
as their first parameter.class Dog: def __init__(self, name, age): self.name = name self.age = age def bark(self): # Instance method print(f"{self.name} says Woof!")