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 Object-Oriented Python (retired) Inheritance Subclasses

What's the difference between subclasses and instances of a class?

They seem to be as similar as to be the same thing. Just curious.

2 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher
class Shape:
    sides = 0

class Square(Shape):
    sides = 4

diamond = Square()
diamond.rotate = 90

Square is a subclass of Shape. diamond is an instance of Square.

Kenneth Love . How do they actually differ? Don't they have the same attributes?

So in this case, Square would be given all the attributes a shape has with the exception that 'sides' is initialized to a difference number. If there was another attribute under the Square class, say 'length', then it would have all the attributes Shape has (that it inherited from) plus it's new attribute 'length'. Diamond on the other hand is an instance of Square. So it is given all the attributes of the Square class ('sides' and 'length').