Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Object-Oriented Python Instant Objects Master Class

1 Answer

Mark Ryan
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Mark Ryan
Python Web Development Techdegree Graduate 28,154 Points

Hi, Jafar!

When we want to make an instance of a Class in Python, we can provide arguments to its init method.

class RaceCar:
    def __init__(self, color, fuel_remaining):
        self.color = color
        self.fuel_remaining = fuel_remaining
racecar = RaceCar("blue", 68)

We can also provide keyword arguments to the init method.

class RaceCar:
    def __init__(self, color, fuel_remaining, doors=None):
        self.color = color
        self.fuel_remaining = fuel_remaining
        self.doors = doors
no_doors_racecar = RaceCar("red", 44)

Above we're saying that our RaceCar instance has no doors by default. What if we want one of our RaceCar instances to have two doors?

class RaceCar:
    def __init__(self, color, fuel_remaining, doors=None):
        self.color = color
        self.fuel_remaining = fuel_remaining
        self.doors = doors
two_door_racecar = RaceCar("red", 44, doors=2)

What if our car may have many keyword arguments that we know about? That's where **kwargs and kwargs.get() comes in.

class RaceCar:
    def __init__(self, color, fuel_remaining, **kwargs):
        self.color = color
        self.fuel_remaining = fuel_remaining
        self.doors = kwargs.get('doors')
        self.seats = kwargs.get('seats')
        self.lights = kwargs.get('lights')
racecar_with_doors_lights_seats = RaceCar("red", 44, doors=2, seats=4, lights=4)

But what if we don't know the characteristics of a car we want to make in the future? What if we want to be flexible with keyword arguments - accept any number of keyword arguments?

class RaceCar:
    def __init__(self, color, fuel_remaining, **kwargs):
        self.color = color
        self.fuel_remaining = fuel_remaining

        for key, value in kwargs.items():
            setattr(self, key, value)
ferrari_racecar = RaceCar("red", 44, doors=2, seats=4, lights=4, make="Ferarri", model="F40")

Above allows our RaceCar class to accept any number of keyword arguments. The kwargs.items() will reveal each keyword argument and its corresponding value. The setattr() will set each keyword argument and its value to your instance. Now your class is more flexible!

I hope this helps!