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]

[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

Barrel Sander (M002) Progress

The Barrel Sander is coming along again, after a small detour to build the (M003) Auto Loading jig.  Here is the nine sides bolted inside the barrel.  They do three things:

  1. Make the barrel stronger so the wheels that it turns on have more support than just thin metal.
  2. Provide a disrupted circle which will cause the materials to actually turn instead of sliding.
  3. Protect the inside of the barrel from the sanding action.

Auto Loading CNC Router Jig

After years of making the same part repeatedly on the CNC router, I finally decided it was time to make the router load it’s own material.

Friction is a valid way to hold a part for routing.  That is how most vices and clamps work.  The key here is to calculate an amount of friction that will resist the cutting forces while at the same time keeping it low enough for the router to be able to overcome it while loading and unloading.

I’ll post more on this when we have it working.  For now, here is a cool picture of the jig.

Fusion360_2018-08-24_00-41-19

Barrel Sander (M002) Motor Assembly

Most standard 60HZ AC motors come in approximately 3600 RPM or 1800 RPM.  The barrel sander needs to spin at about 30 RPM.  To achieve this, the following approximate pulley sizes will be used:

  • 1800 RPM motor
  • Shaft to 2″ Pulley
  • Belt to 7.5″ pulley
  • Shaft to 1.75″ pulley
  • Belt to barrel (approx 23″)
  • End Result is approx 30 RPM

(This was from memory so might be fractionally off).

Here is a render of the pulley assembly (draft):

Fusion360_2018-08-10_01-23-43