Types and Classes
Everything in Python is an object. It’s deja vu all over again. Thanks, Yogi. Each of the objects has a type. The type indicates its nature. A number has the type
int, text has the type
str, and an instance of a person object has the type
type() function on it returns the fact that it is an object instance of the
list class. Here’s one of the cats … and the type of that is
str. Interestingly, it’s turtles all the way down. Here’s the third letter of
"tiger", whose type is also a string.
01:09 That’s a bit different from other programming languages that make a distinction between a character and a string. Strings and their substrings are both strings in Python, right down to single-letter instances. How about when you write your own class?
I’ve kept it simple here, creating an empty class. Here’s an instance … and hopefully that’s not a surprise that the type of the instance is the
Dog class. The
__main__ prefix on this is because I’m in the REPL.
If the class were declared in a module, you’d get the module name as the prefix instead. Let’s try something else. That’s the
max() function from the built-in library and that’s
max’s type. Handy that Python distinguishes built-in types from others, huh?
and get an instance. Goodfeathers shout out for the win. There’s the instance and its type. That shouldn’t be too surprising, as that’s how it was built. Do note that it isn’t calling it a class but an
enum. We’ll come back to that later.
Type of a
list, type of a
float, they’re all
type type is the top level of the class hierarchy. In fact, you can think of the
type() function the same way you think of the
str() function. When you call
str() on something, it returns its
When you call
type() on something, it returns its
type equivalent. It bears repeating,
type is the top of the hierarchy. Let me show you. Yeah, the type of
type, which you get by calling
Python has had classes and objects from almost the beginning, but how they were built changed in Python 2.2. In this version, they added the idea of a base
object that everything can inherit from. Up until Python 3, both the old style and new style of classes was supported.
You can still use the new style declaration inheriting from
object, but if you don’t, Python uses the new style anyways. This saves some typing, but it can cause some confusion for folks who are going back and forth between Python 2 and 3. Hopefully, that’s fewer and fewer of you nowadays.
apple instance. And the
.__class__ attribute of an object indicates what class it’s from. Here it’s showing that
Fruit declared in
__main__. Again, being in the REPL is what causes
__main__ to be the module name.
carrot is an instantiation of
__main__.Vegetable. The only obvious difference between the string representation of the old and new class is the old class includes an object ID reference, and the new one does not.
The type, on the other hand, is quite different. This is the kind of result you saw when I was playing with the
type() function before. The type of a new class object is the object’s class, where the old-style object’s type was
You’ve seen this already. The point I’m trying to make is in Python 3, you no longer need to inherit from
object. Python 3 uses the same syntex as Python 2’s old-style classes, but actually produces new—that’d be 2001-new—style classes.
Love me some gnocchi. Type of the instance is the class, and the type of the class is
type. Essentially, although not necessary, you can inherit from
object in Python 3 and get the same result as not doing it.
Become a Member to join the conversation.