A (very) short breakdown of types
If you are like me and got into programming from a non-traditional background, you probably took a lot of things at face value. When you were told the definition of some computer science topic, it probably sounded too abstract to fully grasp. Pulling from the paper On Understanding Types, Data Abstraction, and Polymorphism by Luca Cardelli, I will attempt to give some semblance of what a type is and how the type system in Go differs from that of Python.
“Types arise informally in any domain to categorize objects according to their usage and behavior.”
Another way to say this, is when you have an “Untyped Universe”, or a field with a bunch of things/objects/items, We can begin to sort these things into categories based on how they interact with other things/objects/items within and between categories.
A good example Cardelli uses is that of bit strings. In a computer’s memory, data is stored via a bit string which represents the data it is encoding. This bit string is a contiguous sequence of 1’s and 0’s. For example, the bit string 110 represents the number 6 when read as an unsigned binary integer. You can classify bit strings in different ways. One such way is creating a set of all 3 length bit strings with a weight of 2 would which would be {110, 101, 011} with each bit string representing 6, 5, and 3 respectively. To the computer, these bit strings are all the same, more or less. But the values these bit strings represent can be further classified based on their behavior with other bit string representations. For example, when read as unsigned integers, 110 and 011 represent 6 and 3, but read as signed integers they represent −2 and 3. We must clearly define what representation we are intending for our operations (say, addition) to be predictable. That is where types come in.
When we begin to organize objects (such as bit strings) into categories according to their usage and behavior, we are creating a type system.
Cardelli gives a great analogy on this very point.
“A type may be viewed as a set of clothes (or a suit of armor) that protects an underlying untyped representation from arbitrary or unintended use. It provides a protective covering that hides the underlying representation and constrains the way objects may interact with other objects.”
In programming languages, there is often a type system that defines how different objects behave with each other within the context of that language.
For example, the type system in JavaScript has defined the following behavior. When two integers are added, the result is an integer.
6 + 3 // 9
When an integer is added to a string, a slightly different behavior emerges, and the result is a string.
"6" + 3 // "63"
And when you subtract an integer from a string, an integer is returned once again.
"6" - 3 // 3
The point of this illustration is not to evaluate JavaScript type semantics, but to illustrate that JavaScript has intentionally defined what happens when 2 different objects (in this case, an integer and a string) interact with each other. Go and Python have similarly defined type systems which we will explore now.
Numeric Types
Python has 3 main numeric types: float, int, and complex. Python does something called automatic type promotion meaning if you add 2 of different numeric types, the interpreter will change one into the other behind the scenes, specifically, something called “widening”. The below is an example of widening:
>>> 6 + 3.0
9.0
In Go, there is no type coercion or widening. You cannot add 2 different numeric types together. For example, you would get an error if you tried to compile the example below.
var x int = 6
var y float64 = 3.0
z := x + y
You would need to convert the float to an int, or the int to the float.
var x int = 6
var y float64 = 3.0
z := x + int(y)
In Python, / always produces a float and // is floor division.
>> 7 / 2
3.5
>> 7 // 2
3
In Go, dividing two ints gives an int.
>> 7 / 2
3
Truthiness
In Python, the value of a type can be treated as a boolean and thus be tested for “truthiness”". A common example of this is with lists. Often we want to check if a list is populated. We can do so like so.
>>> my_list = [1, 2, 3, 4]
>>> if my_list:
... print("truthy")
"truthy"
There is no notion of truthiness or falsiness in Go, and you cannot convert any other type to a boolean value, either implicitly or explicitly.
Constants
Constants in Go cannot be reassigned.
const x = 5
x = 6 // compile time error
In Python, constants are just a convention. They can be reassigned at any point.
X = 5
X = 4 # Okay
The convention in Python is to use uppercase variables to denote a constant, while in Go constants are denoted by the const keyword.
You can annotate your code with type hints in Python so that static checkers, like Mypy, can flag if you reassign a variable meant to be a constant. But the interpreter won’t complain (your program will still run).
import typing
X: typing.Final = 5
X = 6 # Mypy will fail
I think this is a good place to stop. Composite types will likely each need their own treatment. If you want to see my conflicted love hate relationship with Python types, checkout this article here Cheers~