Instance attributes and methods
Instance attributes are pieces of information specific to each object (or instance) of a class. Think of them as the "personal details" of an object. For example:
- A dog has a name and a breed.
- A car has a color and a brand.
Each object has its own unique set of attributes.
How do you define instance attributes?
You define them inside the __init__
class Dog:
def __init__(self, name, breed): # Initialize attributes
self.name = name # Instance attribute for the name
self.breed = breed # Instance attribute for the breed
In this example:
andself.name are instance attributes.self.breed
- The
andname
values are provided when creating abreed
object.Dog
How to access instance attributes?
You can access instance attributes using the dot .
# Creating an object
my_dog = Dog("Buddy", "Golden Retriever")
# Accessing attributes
print(my_dog.name) # Output: Buddy
print(my_dog.breed) # Output: Golden Retriever
Each object (instance) keeps its own information. If you create another dog, it will have its own unique attributes:
another_dog = Dog("Max", "Beagle")
print(another_dog.name) # Output: Max
print(another_dog.breed) # Output: Beagle
What are instance methods?
Instance methods are actions that an object can perform. These methods usually work with the instance attributes of the object.
How do you define instance methods?
To define an instance method, you write a function inside the class. The first parameter of the method is always self
class Dog:
def __init__(self, name, breed):
self.name = name
self.breed = breed
def bark(self): # Instance method
return f"{self.name} says Woof!"
Here’s what’s happening:
is an instance method.bark(self) - It uses
to access the name of the specific dog.self.name
How to use instance methods?
You call an instance method using the dot .
# Creating an object
my_dog = Dog("Buddy", "Golden Retriever")
# Calling the instance method
print(my_dog.bark()) # Output: Buddy says Woof!
If you create another dog, it can use the same method but with its own attributes:
another_dog = Dog("Max", "Beagle")
print(another_dog.bark()) # Output: Max says Woof!
Putting it all together
Here’s a full example with attributes and methods:
class Car:
def __init__(self, brand, color):
self.brand = brand # Instance attribute
self.color = color # Instance attribute
def start(self): # Instance method
return f"The {self.color} {self.brand} is starting!"
# Create objects
car1 = Car("Toyota", "red")
car2 = Car("Tesla", "blue")
# Access attributes
print(car1.brand) # Output: Toyota
print(car2.color) # Output: blue
# Call methods
print(car1.start()) # Output: The red Toyota is starting!
print(car2.start()) # Output: The blue Tesla is starting!
Key points to remember:
Instance Attributes:
- Defined in the
method using__init__
.self
- Unique to each object (e.g., each dog has its own name and breed).
Instance Methods:
- Functions inside the class that use the object’s data.
- Always have
as the first parameter.self
- Called using the dot
operator..