curriculum/challenges/english/blocks/lecture-classes-and-objects/6874b5fc38f90f25e18093ce.md
In the last lesson, you learned about classes and how they act as blueprints for creating objects.
Here, we will dive deeper into attributes and methods.
Let's take a closer look at attributes first, then methods.
Attributes are variables that belong to an object, so they hold data. There are two kinds of attributes: instance attributes and class attributes.
Instance attributes are unique to each object created from a class, and you usually set them with the __init__ method. Class attributes, on the other hand, belong to the class itself and are shared by all instances of that class.
To access an attribute, you use dot notation.
Here are examples of both instance and class attributes, and how to access them from objects:
class Dog:
species = "French Bulldog" # Class attribute
def __init__(self, name):
self.name = name # Instance attribute
print(Dog.species) # French Bulldog
dog1 = Dog("Jack")
print(dog1.name) # Jack
print(dog1.species) # French Bulldog
dog2 = Dog("Tom")
print(dog2.name) # Tom
print(dog2.species) # French Bulldog
Note that you can access class attributes directly from the class itself, but you need to create an object and pass it data first before you can access instance attributes.
Cars are another good example, since all cars have a model and color:
class Car:
def __init__(self, color, model):
self.color = color
self.model = model
car_1 = Car("red", "Toyota Corolla")
car_2 = Car("green", "Lamborghini Revuelto")
print(car_1.model) # Toyota Corolla
print(car_2.model) # Lamborghini Revuelto
print(car_1.color) # red
print(car_2.color) # green
Methods are functions defined inside a class. With them, any object defined from a class can perform actions that operate on or modify its own data. You also access a method with dot notation.
For example, dogs can bark. So we can have a bark method in the Dog class like you saw in a previous lesson:
class Dog:
species = "French Bulldog"
def __init__(self, name):
self.name = name
def bark(self):
return f"{self.name} says woof woof!"
jack = Dog("Jack")
jill = Dog("Jill")
print(jack.bark()) # Jack says woof woof!
print(jill.bark()) # Jill says woof woof!
A Car class can also have a describe method:
class Car:
def __init__(self, color, model):
self.color = color # Instance attribute
self.model = model # Instance attribute
def describe(self):
return f"This car is a {self.color} {self.model}"
car_1 = Car("red", "Toyota Corolla")
car_2 = Car("green", "Lamborghini Revuelto")
print(car_1.describe()) # This car is a red Toyota Corolla
print(car_2.describe()) # This car is a green Lamborghini Revuelto
What are the two types of attributes in Python?
Public and private attributes
Think about the two ways you can define attributes.
Local and global attributes
Think about the two ways you can define attributes.
Instance attributes and class attributes
Mutable and immutable attributes
Think about the two ways you can define attributes.
3
What is required to access instance attributes?
The class name only
Think about how you access class attributes versus how you access instance attributes.
Decorators
Think about how you access class attributes versus how you access instance attributes.
An instance or object of the class
A static method
Think about how you access class attributes versus how you access instance attributes.
3
How do you define and access methods?
As standalone functions outside a class, accessed with brackets
They belong to the class but operate on object data.
As variables in a class, accessed like attributes
They belong to the class but operate on object data.
With special keywords, called automatically
They belong to the class but operate on object data.
They are defined inside a class and accessed with dot notation
4