[Chicago] Python for mortgages???

Chris Foresman foresmac at gmail.com
Tue May 19 00:54:01 CEST 2015

It might be helpful to think of “x” in your example as a named reference to an object of type int with the value 5. Later, you tell the runtime to use “x” as a named reference to an object of type str with the value “Douglas”. When there are no longer any references to the object of type int with value 5, it will basically be garbage collected.

There are some implementation details here that might be interesting. The “id” of each object is unique, and in C Python is the address of the object in memory (pointer)[1]. Also, all integers up to 255 (IIRC, might be slightly different) have fixed memory locations as an optimization since they are so often used[2].

Check out this REPL output:

``` python
Python 2.7.6 (default, Sep  9 2014, 15:04:36) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 5
>>> type(x)
<type 'int'>
>>> id(x)
>>> id(5)
>>> x = "Douglas"
>>> type(x)
<type 'str'>
>>> id(x)
>>> id("Douglas")
>>> del(x)
>>> x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
>>> id("Douglas")
>>> for i in range(10):
...     print id(i)

Note that even though I’ve “deleted” x, the object it refers to is still in memory. It hasn’t been garbage collected and deallocated yet. Also note how the location of the object of type int with value 5 is the same as x when x = 5. Also note how memory locations for small ints are sequential (generated when starting the interpreter) and that the size of an int object in Python is 24 bytes.

1. https://docs.python.org/2/library/functions.html#id <https://docs.python.org/2/library/functions.html#id>
2. https://jakevdp.github.io/blog/2014/05/09/why-python-is-slow/ <https://jakevdp.github.io/blog/2014/05/09/why-python-is-slow/>

Chris Foresman
chris at chrisforesman.com

> On May 18, 2015, at 11:09 AM, Lewit, Douglas <d-lewit at neiu.edu> wrote:
> But in Python you can!  Which is so cool!  You can do this:
> x = 5
> (But later in the program.)
> x = "Douglas"
> No problem.
> I guess I'm a little confused by the difference between static vs. dynamic and weak vs. strong typing.  Okay Rob, I need a lesson here!   :-)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20150518/ea910d42/attachment.html>

More information about the Chicago mailing list