Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Python class - object

What does the object inheritance mean here - class Monster(objeect): What does object class contain and how,why is it useful?

1 Answer

Syntax like this makes the Monster-class inherit all the same properties that object has. It is quite useful if - for example - you have to construct a lot of similar classes with minor differences. Instead of repeating a lot of the code you just create instances from the one object you have made.

Lets say that we have a class of "car", with all the most standard car-features:

class car: 
    seats = 5
    tyres = 4
    fuel = "gasoline"
    doors = 4
    ac = True
    # and so on.

Now we have a class that describe most cars (presumably), but not every car. But it still would save us a lot of time if we were planning to describe every type of car. Say we wanted our program to include electric cars, then we only have to change the one variable, instead of re-typing the whole class again, we just have to do this :

class electric_car(car):
    fuel = "electric"
    seats = 5  # inherited from car 
    tyres = 4  # inherited from car
    doors = 4  # inherited from car
    ac = True  # inherited from car

Now vision you are making a lot bigger and more complex classes with a mix of similarities in them and various functions and so on, it would save you a ton of typing.

I hope this helped. Good luck.