## Thursday, March 31, 2011

### every string is an iterable of strings

>>> 'a'[0][0][0][0][0]
'a'
Python does not actually create a bunch of string objects when you do this.

>>> a = 'a'
>>> id(a) == id(a[0][0][0][0])
True

## Thursday, March 17, 2011

### Negative Integer Gotchas (mod and div)

Quick!  What are the values of these expressions: -5%100, -5/100?
If you said -5 and 0, you'd be right... if this was C or Java.
>>> print -5%100, -5/100
95 -1
Some more fun with negative modulos.
>>> 3%-1
0
>>> 3%-2
-1
>>> 3%-3
0
>>> 3%-4
-1
>>> 3%-5
-2
>>> 3%-6
-3
>>> 3%-7
-4
Why does Python have a different behavior than C, Java, Fortran et al?
The decree of the Benevolent Dictator For Life!

## Monday, March 14, 2011

### Local Variable Performance & dis

Local variables are faster in python.  They are accessed as offsets from a stack, rather than looked up in a hashmap.

Here is an example showing the use of local variables as a performance optimization.  http://wiki.python.org/moin/PythonSpeed/PerformanceTips#Local_Variables

Using the dis module, we can investigate this behavior.  Note LOAD_FAST versus LOAD_DEREF.

>>> import dis
>>> def foo():
...   a = 1
...   def bar():
...      return a
...   return bar
...
>>> b = foo()
>>> dis.dis(b)
4           0 LOAD_DEREF               0 (a)
3 RETURN_VALUE
>>> def loc():
...    a = 1
...    return a
...
>>> dis.dis(loc)
2           0 LOAD_CONST               1 (1)
3 STORE_FAST               0 (a)

3           6 LOAD_FAST                0 (a)
9 RETURN_VALUE