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
Benjamin Hedgepeth
5,672 PointsClarification between type and class
I've read that type and class in Python are synonymous terms. In Wikipedia they're defined as follows:
a data type or simply type is a classification identifying one of various types of data, such as real, integer or Boolean, that determines the possible values for that type, the operations that can be done on values of that type, the meaning of the data, and the way values of that type can be stored.
a class is an extensible program-code-template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods)
Type makes total sense to me. When someone says that type and class are the same thing, but I go off these definitions, I don't see it. Unless, "implementations of behavior" refers to the operations that can be performed on a particular type.
Any clarification would be much appreciated. Thanks.
2 Answers
Chris Freeman
Treehouse Moderator 68,468 PointsThe Wikipedia definitions you show for data type and class are correct in a classical computer science sense. And for early Python (pre-2.2) these held up as types referred to built-in types such as lists, dictionaries, integers, etc. and classes referred to user-defined classes. These older style classes, now referred to as "classic classes", did not derive from or inherit from a built-in base object class.
Starting in Python 2.2, a "new-style class" was defined that inherited from the base built-in class object by default. With this unifying change, all new-style classes became the same "thing" as the built-in types, thus making all classes types. Now, for Python, the words can be used interchangeably though some still use "class" for user defined objects and "type" for built-in types.
Here is good read on this written by Guido van Rossum
Steven Parker
243,658 PointsThe concepts of class and type are clearly not the same thing. But when identifying what a specific variable represents, a class can be considered the same as a type. I'd bet that where you read they are the same, it was in the context of identifying a variable.
Benjamin Hedgepeth
5,672 PointsThat was where some of my thinking was too. Your post reinforced my hunch when it came to understanding this.
Chris Freeman
Treehouse Moderator 68,468 PointsI would disagree. Please see my response. Today, the terms are used for conversation semantics, but there isn't a real difference between type and class.
Benjamin Hedgepeth
5,672 PointsBenjamin Hedgepeth
5,672 PointsPlease elaborate on the part of 'base built-in class object'.
Chris Freeman
Treehouse Moderator 68,468 PointsChris Freeman
Treehouse Moderator 68,468 PointsIn Python 2, to get the new-style classes, the class declaration would need to inherit from the base class
object:In Python 3 this is done by default, but could be stated explicitly if desired:
More info in the docs