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?
3 Answers 3
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.)
13 Comments
int type is done in C code, as opposed to user-created classes in Python code.type and class are interchangeable, but in older Python versions there was a difference still in that you could not mix the two.type vs class. I am referring to 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).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.
5 Comments
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.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?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 type checker (or you) can use 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.
4 Comments
"" + 0 is a type error, unlike JS), but also dynamic (so, that type error happens at runtime, not compile time).python+mypy be as statically typed as they come? AFAIK typically people like to use "strong typing" to refer to the fact that a compilable program should have no runtime type related errors. In C++ you can get around the compiler using (void*) casting or whatever. C# is much closer to this idealism, but there are probably a few trapdoors you can get around the compiler with.
object. In Python3, all classes inherit fromobject, so they are the same thing.