Programmer's Python - Class & Type
Written by Mike James   
Monday, 08 February 2021
Article Index
Programmer's Python - Class & Type
Class Based Type
Hierachical Typing

They are both numbers but as far as the computer is concerned they are different types of number.

More subtle and confusing to beginners is that:


looks like a number but it is represented as a character. Put simply, the bit pattern that represents integer 3 and character “3” are quite different.

Beginners often find the distinction between a character string that looks like a number and a number as being a silly one. They also often regard the distinction between floating point and integer as equally silly and many languages do their best to cover up the internal representations.

Their philosophy can be summed up as:

If it looks like a number then it behaves like a number, and an integer is just a floating point that doesn’t have a fractional part.

To an experienced programmer this is seen as a backward step but it isn’t. Why should the average working programmer be bothered with the details of how data is represented and stored? It is a move towards abstraction to regard everything that looks like a number as a number, and not to distinguish different types of number, even.

Of course these differences still exist but its up to the compiler to deal with them and shield us from them.

Python doesn’t do this but it is still slightly different from other languages. It has three types of number, integers, floating point and complex numbers. It also distinguishes between general integers and booleans which are restricted to be 0 or 1.

Python integers are strange in that they work with an arbitrary number of digits. You can use:


and you can keep typing digits and the arithmetic will be done accurately. Most other languages have a very fixed limit on the number of digits you can use and you can write arithmetic expression that can overflow the representation i.e. you can get a result that is too big to represent.

Of course in practice there will be a limit and you will get an overflow but it varies according to the hardware and Python system.

Similarly, Python’s floating point numbers are implemented to fit in with the hardware or the operating system. Generally double precision floating point IEEE standard numbers.

It is worth pointing out that Python doesn’t make a distinction between primitive data types and other data types. Remember, everything in Python is an object and everything is on an equal footing.

Of course for reasons of efficiency this is mostly an illusion and integers, floats and complex numbers are implemented differently from a general object. These are matters of implementation and you are better off ignoring them as much as possible.

The point is that this is where the idea of type originates. At its most basic a data type is a way of representing something, and it is that representation that determines what operations you can use without having to convert to some other type.

For example you can perform arithmetic on numbers but not on characters that just happen to look like numbers.

Put simply, type tells you the operations that you can perform.

By declaring the type of an object you specify exactly what operations you can use and what methods you can call.

Class-Based Type

Now it is time to move on to the much wider concept of type that you find in all strongly-typed, class-based languages. In what follows the examples are given in a C++ or Java like class-based language to contrast with Python.

In most class-based languages, classes are considered to be types and not objects.

In a class-based language declaring a class also creates a new data type.

That is: 

Class MyClassA(){
  lots of properties

not only creates a new class, it also adds the new data type MyClassA to the type system.

In this system of doing things objects are of a particular type, and variables have to be declared as a particular type, and a variable can only reference objects of that type.

Now when you declare a variable of the type:

MyClassA myVariable;

the system knows what myVariable is referring to.

This allows the system to check that when you write:


that myProperty is indeed a property that is defined on the type. If it isn't then you get a compile-time error which you can correct before it throws a runtime error.

Contrast this with Python or any untyped language where myVariable can reference any object and hence you cannot deduce that:


is valid simply by reading it.

You can usually deduce its validity by reading the rest of the program, however. Strong typing makes this aspect of static analysis easier but this comes at a cost.

Notice that type occurs in two ways – a variable has a declared type and an object is of a particular type. 

At its simplest the strong typing simply enforces the rule that a variable can only reference an object of its declared type.

That is, an instance of a class has a type and only a variable of the same type can reference it.

Last Updated ( Monday, 08 February 2021 )