39

I just recently started to teach myself how to code. I am currently reading Think Python 2 for python 3 and when it teaches about the type() function, it gives the example type(2) which outputs <class 'int'>. It then states that "the word 'class' is used in the sense of a category; a type is a category of values."

The part that confuses me is that the type() function outputs class instead of type. Also, I'm not sure about the difference between type and class; are string, float point, and integer classes of the type "value", or are they the same thing?

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Jeff
  • 511
  • 1
  • 4
  • 7
  • 6
    Python 3 completed the unification of types and classes (which [started way back with Python 2.2](https://www.python.org/download/releases/2.2.3/descrintro/)). Your book attempts to explain a difference that isn't really there anymore. – Martijn Pieters Mar 12 '16 at 14:38
  • 1
    Basically a type is class that inherits from `object`. In Python3, all classes inherit from `object`, so they are the same thing. – zondo Mar 12 '16 at 14:40
  • 1
    @zondo: that's not quite accurate either. It is related, but that's not a classification I'd use. – Martijn Pieters Mar 12 '16 at 14:45
  • Maybe find another book... – Rick Mar 12 '16 at 14:47
  • @RickTeachey, what book would you suggest then? – Jeff Mar 12 '16 at 14:51
  • The book I learned with, Learn Python the Hard Way, is a little old now since it was written for python 2.6. I'm not sure what good beginner books out there are worth using since I haven't been a beginner for a while, but I would suggest something that was written with Python 3 in mind. – Rick Mar 12 '16 at 15:30

3 Answers3

43

Once upon a time, Python had both types and classes. Types were built-in objects defined in C; classes were what you built when using a class statement. The two were named differently because you couldn't mix these; classes could not extend types.

This difference was artificial, a limitation in the language implementation. Starting with Python 2.2, the developers of Python have slowly moved towards unifying the two concepts, with the difference all but gone in Python 3. Built-in types are now also labelled classes, and you can extend them at will.

Your book is trying to explain a difference that isn't present in Python anymore. Even in Python 2 the difference is only there in name, since type(2) shows the word 'type' is still used there:

>>> type(2)
<type 'int'>

but you can subclass int just like any other class.

(Python 2 does still have old-style classes, those that don't inherit from object; these are a remnant of the old system from before the unification.)

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    Can you elaborate on what you mean by "defined in C?" So in Python 2, the type would be integer and the class would be 2? – Jeff Mar 12 '16 at 15:01
  • 1
    @Jeff: For CPython (the default Python implementation you download from python.org), the implementation for the `int` type is done in C code, as opposed to user-created classes in Python code. – Martijn Pieters Mar 12 '16 at 15:02
  • @Jeff: `2` is an *instance* of the type `int`. There are no classes involved there. But `class Foo: pass` defines a user-defind class object. `Foo()` would be an instance of that class. – Martijn Pieters Mar 12 '16 at 15:03
  • 1
    @Jeff: basically, the terms `type` and `class` are interchangeable, but in older Python versions there was a difference still in that you could not mix the two. – Martijn Pieters Mar 12 '16 at 15:04
  • Just to make sure I understand everything correctly, type was something defined by Python whereas a class was user created. In python 3, this doesn't matter because types and classes are unified. When you say C code, are you referring to C#/C++ or something else. I appreciate the help, thank you. – Jeff Mar 12 '16 at 15:11
  • 1
    @Jeff: Correct on `type` vs `class`. I am referring to [C programming language](https://en.wikipedia.org/wiki/C_(programming_language)). C# and C++ both have been *named after C* (and both borrow heavily from that language as a basis for their own design). – Martijn Pieters Mar 12 '16 at 15:15
  • So, does this mean that Python is built of C Programming Language? So when I hit execute, the interpreter is not taking what I have written and making it machine code, but rather translating/compiling to C which is then changed to machine code? Sorry to keep asking question. – Jeff Mar 12 '16 at 15:35
  • If the difference is "all but gone" in Python 3, that means there is still some tiny vestige of difference left. I am completely confident I will never encounter this difference myself in my own code, but I'm still interested in what that tiny bit of difference is, for an academic conversation I'm having with a language enthusiast. What's that difference, or where can I read about it? – John Y Oct 11 '18 at 17:42
  • @JohnY: just terminology; `type()` is metaclass object that produces classes; `type(instance)` gives you the class of the object. The terms have become interchangeable almost. That's it. – Martijn Pieters Oct 11 '18 at 19:13
  • @MartijnPieters if you could please go a bit further with clarifying internals, so things like `2` and `'abc'` are instances of their types, i.e `int` and `str`, so they are python objects that have methods, attributes, addresses, etc. If you were to do something like `import sys; sys.getsizeof('a')` you won't get 1 byte as as one would properly expect for utf-8 encoded strings but where are the actual values stored? like for instance the numerical value of `2`, or the numerical value of `'abc'` (since everything boils down to hex and binary in the end) ? how are they stored and used ? – Marius Mucenicu May 16 '19 at 18:17
  • @George: sorry, not in comments here. That's been answered elsewhere here on Stack Overflow, by me and others. – Martijn Pieters May 17 '19 at 08:22
  • @MartijnPieters sure thing, sry...I'll dig deeper for those answers! I'm actually curious about how that works internally. – Marius Mucenicu May 17 '19 at 08:50
  • @MartijnPieters I really appreciate this explanation, because PEP 483 had me confused, as it is asserting "the distinction between classes and types" https://www.python.org/dev/peps/pep-0483/#types-vs-classes – Zubo May 09 '21 at 22:30
5

The python hierarchy is Type (Metaclass) -> Class -> Instance. Think of the function type() as going one level up.

If you use the function type() on an instance of an int (any integer) like so: type(123) you will receive the class of the instance which in this case is int. If you will use type() on the class int, you will recieve type type which is the metaclass of int.

Keep in mind metaclasses are advanced technical details of python and you do not need to learn about them at first.

Bharel
  • 23,672
  • 5
  • 40
  • 80
  • 9
    The text in question is not referencing metaclasses. `type` is a metaclass, yes, but `type(someobject)` doesn't return the metaclass, it returns the class for that object. Bringing metaclasses into this only serves to confuse the matter further, I fear. – Martijn Pieters Mar 12 '16 at 14:47
  • 2
    True that it's not referencing metaclasses but keep in mind type(obj) does return the metaclass of the object in case the object is itself a class. It's causes a bit of confusion, I agree, and that's why I wrote that it's more advanced but the solution is more complete like that I believe. – Bharel Mar 12 '16 at 14:50
  • 3
    Yup, and then there is the fun with `type(type)`, etc. But that's all *too much information* that isn't really relevant to the question *what is the difference between a class and a type*? – Martijn Pieters Mar 12 '16 at 14:51
  • Alright, thanks for the input, will be more careful next time :-) – Bharel Mar 12 '16 at 14:56
0

If you are just starting to learn about programming, keep in mind that the terms "type" and "class" are heavily overloaded (used for sometimes different, sometimes synonymous things in different contexts). Rougly speaking, you have...

  • The abstract notion of type. This is something that does not exist when the program runs, but a compiler (or you) can use them to identify invalid uses of objects
  • In many languages, including python, there is a runtime representation of the types. Using these representations to alter runtime behavior is called "reflection."
  • Then there are classes. So typically (in math), a type refers only to the structure of the data (i.e. fields and their types). In Object Oriented Programming, it is typically necessary to keep the definitions of certain function local to the definition of the class to enforce "data-hiding." Anyways, the end result is that we can conceptualize a class as a sort of "type McMenu". You get a type (data-structure), interface (public methods), and constructor (usually given by some special syntax or convention - e.g. __init__).
  • Finally, bear in mind that there are static and dynamic types of expressions. The distinction that is relevant to programmers here is the notion of "virtual functions." These don't feel that impressive in python, but in strong-er-ly typed languages like C++ and C# this is rather powerful and helpful.
Nathan Chappell
  • 2,099
  • 18
  • 21