Python Mutable vs Immutable

Here is a basic introduction to immutable and immutable types in python.

In python there are two types of data… mutable and immutable. Numbers, strings, boolean, tuples, and other simple types are immutable. Dicts, lists, sets, objects, classes, and other complex types are mutable.

When you say:

a = [1,2,3]
b = a

You’ve created a single mutable list in memory, assigned a to point to it, and then assigned b to point to it. It’s the same thing in memory.

Therefore when you mutate it (modify it):

b[0] = 3

It is a modification (mutation) of the index [0] of the value which b points to at that same memory location.

However, when you replace it:

b = [0,0,0]

It is creating a new mutable list in memory and assigning b to point at it.


Check out the id() function. It will tell you the “address” of any variable. You can see which names are pointing to the same memory location with id(varname).


Bonus: Every value in python is passed by reference… meaning that when you assign it to a variable it simply causes that variable to point to that value where it was in memory. Having immutable types allows python to “reuse” the same memory location for common immutable types.

Consider some common values when the interpreter starts up.  You can see here there are a lot of variables pointing at the memory location held by abc.  cpython, at least, is smart enough to realize that the value `abc` is already stored in memory and because it is immutable, just returns that same memory address.

>>> import sys
>>> sys.getrefcount('abc')
68
>>> sys.getrefcount(100)
110
>>> sys.getrefcount(2)
6471

However, a value that is definitely not present would return 2. This has to do with the fact that a couple of references to that value were in-use during the call to sys.getrefcount

>>> sys.getrefcount('nope not me.  I am definitely not here already.')
2

Notice that an empty tuple has a lot of references:

>>> sys.getrefcount(tuple())
34571

But an empty list has no extra references:

>>> sys.getrefcount(list())
1

Why is this? Because tuple is immutable so it is fine to share that value across any number of variables. However, lists are mutable so they MUST NOT be shared across arbitrary variables or changes to one would affect the others.

Incidentally, this is also why you must NEVER use mutable types as default argument values to functions. Consider this innocent little function:

>>> def foo(value=[]):
...     value.append(1)
...     print(value)
...
...

When you call it you might expect to get [1] printed…

>>> foo()
[1]

However, when you call it again, you prob. won’t expect to get [1,1] out… ???

>>> foo()
[1, 1]

And on and on…

>>> foo()
[1, 1, 1]

>>> foo()
[1, 1, 1, 1]

WHY IS THIS? Because default arguments to functions are evaluated once during function definition, and not at function run time. That way if you use a mutable value as a default argument value, then you will be stuck with that one value, mutating in unexpected ways as the function is called multiple times.

The proper way to do it is this:

>>> def foo(value=None):
...     if value is None:
...         value = []
...     value.append(1)
...     print(value)
...
...
>>>
>>> foo()
[1]
>>> foo()
[1]
>>> foo()
[1]

[M007] Paint Thinner Filter

As we clean up wood stain from brushes and equipment it contaminates the paint thinner used in the parts washer.  The good news is that most of the stain settles to the bottom.

Here is a “funnel” that we designed and had fabricated by Anything Metal in Altoona.  It will allow the solids to flow to the bottom overnight, and then we can draw them off and properly dispose of, while dramatically reducing the amount of paint thinner we need to dispose of.

 

43641648_169389894000073_7346682748267921408_n.png

2018-10-12 - 183539.png

 

2018-10-12 - 183552.png

[M004] Double Edge Router Assembled!

One evening of CAD.  One evening of CAM.  One evening of CNC.  And it fit together (quite well).  This is just held together with precisely located wooden dowel pins.  We will add screws tomorrow.

The only missing piece is the actual guide for running the blocks through.  That is separate so that it can be “dialed in” to the perfect cutting depth.

20180914_004625.jpg

 

 

 

 

 

 

[M004] Double Edge Router

Working on another machine to speed up yet another tedious workshop process.  Think of it as a special router table that takes 2 routers set to 45 degree angles.  The guide (not shown) will allow precise alignment of the work piece AND cover the cutters to prevent injury.

explorer_2018-09-11_23-52-20