Class attributes and methods
Class attributes are shared by all objects of a class. Think of them as common features that apply to every object created from the class.
Imagine a class for creating
- All cats are mammals.
Instead of giving each cat object the same "mammal" information, we can store it at the class level. This makes it easy to access and saves space.
How do you define class attributes?
Class attributes are defined outside any methods, directly under the class definition. For example:
class Cat:
# Class attribute
species = "Mammal"
is a class attribute because it belongs to thespecies class and is the same for allCat
objects.Cat
Accessing class attributes
You can access class attributes:
-
Using the class name:
print(Cat.species) # Output: Mammal
-
Using an object:
my_cat = Cat() print(my_cat.species) # Output: Mammal
All objects share the same class attribute. Changing the class attribute affects all objects:
Cat.species = "Feline"
print(my_cat.species) # Output: Feline
What are class methods?
Class methods are actions related to the class itself, not just individual objects. These methods:
- Use the
decorator.@classmethod
- Take
(short for class) as the first parameter instead ofcls
.self
Why use class methods?
Class methods are useful when you want to:
- Modify or interact with class-level data (like class attributes).
- Create objects in a specific way (we’ll see an example).
Defining class methods
Here’s how to define a class method:
class Cat:
species = "Mammal" # Class attribute
@classmethod
def set_species(cls, new_species):
cls.species = new_species # Modify the class attribute
- The
decorator tells Python it’s a class method.@classmethod
refers to the class itself, similar to howcls refers to the object.self
Using class methods
You call class methods using either the class name or an object:
# Call using the class name
Cat.set_species("Feline")
# Call using an object
my_cat = Cat()
my_cat.set_species("Big Cat")
print(Cat.species) # Output: Big Cat
When you change the class attribute through a class method, it affects all objects.
Class methods to create objects
Class methods can also be used to provide alternate ways to create objects. For example:
class Dog:
species = "Mammal"
def __init__(self, name, age):
self.name = name
self.age = age
@classmethod
def puppy(cls, name):
# Create a "puppy" object with a default age of 1
return cls(name, 1)
# Create objects using the class method
puppy = Dog.puppy("Buddy")
print(puppy.name) # Output: Buddy
print(puppy.age) # Output: 1
- The
puppy
class method creates a object with a default age ofDog
1
.
Key differences: instance vs. class
Feature | Instance | Class |
---|---|---|
Attributes | Unique to each object | Shared by all objects |
Methods | Use self |
Use cls |
When to Use? | For object-specific data | For shared data or alternate constructors |
Example: class attributes and methods together
class Car:
# Class attribute
wheels = 4
def __init__(self, brand, color):
self.brand = brand # Instance attribute
self.color = color # Instance attribute
@classmethod
def change_wheels(cls, num):
cls.wheels = num # Modify class attribute
# Access class attribute
print(Car.wheels) # Output: 4
# Create objects
car1 = Car("Toyota", "red")
car2 = Car("Tesla", "blue")
# Change class attribute using class method
Car.change_wheels(6)
print(car1.wheels) # Output: 6
print(car2.wheels) # Output: 6
print(Car.wheels) # Output: 6
Key points to remember:
-
Class attributes:
- Shared by all objects of a class.
- Define common data (e.g., species = "Mammal"
).
-
Class methods:
- Perform actions on class-level data.
- Use @classmethod
cls