Understanding vs Knowing

Understanding vs Knowing

Understanding a concept is great. You can explain it to others as well as apply its principles when you need.

But, would you be able to create the concept from scratch if you had to?

Understanding computer memory

Back in 2011 or so, I was studying assembly language and the C programming language. It was pretty boring and I never wanted to build my own programming language/kernel/OS so, I eventually stopped and picked up Python instead.

One concept that I had the chance to understand is how memory is represented in a computer. Basically, blocks of memory are reserved depending on the data type that you’re trying to store. In assembly, you have to manually load these blocks into the CPU registers in order to work with them. In C, you use a pointer to access a particular block. A common use case is to use a pointer to access the block where an array is located so that you can work with the elements in that array.

In Python, the concept of memory is completely useless. Actually, I would say that understanding how memory works will make you less productive. Why? Because instead of focusing on business rules and logic, you’ll worry about the tiny little details but I digress.

If someone asked me to create a string in assembly today, I wouldn’t be able to because I don’t know anything about it. I understand that strings are simply arrays of characters where each character typically requires 8 bytes of memory but again, I don’t know how it works.

Is knowing a concept necessary?

Ok so, I don’t know how to implement strings in assembly so I’m ignorant and I need to learn it right? Well, as a Python programmer, let’s see if knowing how to deal with memory actually helps me at all.

Here’s some simple Python code:

a = 1
b = a
a = 2

What will b evaluate to? If you said 2 then you’re wrong. When you initialize a with the value 1, a block of memory is reserved that will store an int object. This object contains metadata about the object itself as well as the data (in this case, 1). a is a pointer to this object. When you declare a new variable b and set its value to a, b is simply a pointer to the object that a is pointing to.

Later, when we set a to equal to 2, a new block of memory is reserved that will hold a new int object. b will continue to point to the old memory address while a will now point to this new memory address. a and b are now two completely different entities. You can use the id method to confirm this.

Now, check this out:

a = {'a': 1}
b = a
a['b'] = 2

What will b evaluate to? If you said {'a': 1} then you’re wrong. Printing b will output {'a': 1, 'b': 2}. Why is that?

When a is initialized with the value of the dict, a block of memory is reserved that contains a dict object. dicts are simply fancy lists where the keys and values sit in their own blocks of memory. Here, the key a is pointing at the value 1, which is an int object. The dict object itself is simply a pointer to its contents. When you set b to equal to a, you’re creating a pointer to that dict object. Later, when you add a key b to this dict, both a and b are still pointing at the same dict object.

In general, dicts reserve some memory beforehand so that we can add keys without having to reserve entirely new blocks of memory whenever we need to modify it. That’s why you might have heard about dicts being mutable.

You don’t know X

The creator of Python needed to know the ins and outs of computer memory in order to be able to create ints and dicts. But do people who only code in Python need to know about all these intricate details?

I would argue that going beyond a simple understanding could actually hurt you because C was created so that people would no longer have to write assembly. Then Python was created so that people would no longer have to write C.

The reason why Python makes it so much easier to build userland apps like web apps and shell scripts is because you can think at a much higher level than in lower level languages. Linus Torvalds for example, said that he could never build a website. I suspect it’s because he can only think in bit and bytes. But that’s fine. He can build Linux while I can use Linux to host my web apps

Understanding how a dict works is great but I can’t think if any reason why anyone would want to know how to implement their own dict. If you do end up implementing a dict, it will mostly likely have poor performance or just be unusable in general.

Since I don’t actually know about dicts and only understand how they work (I think), my explanation of the above might even be wrong. But does it matter?

Whenever you feel like you don’t know X and that you need to hit the books, think hard about how knowing these intricate details will help you in your work or life. If you can’t find any real benefits, a high level understanding is fine.