[Edu-sig] Code to Joy in The Economist (June/July 2018)

kirby urner kirby.urner at gmail.com
Sun Jun 3 17:54:52 EDT 2018


Jake VanderPlas gets into the "bucket" versus "pointer" discussion in his
Whirlwind Tour:

https://jakevdp.github.io/WhirlwindTourOfPython/03-semantics-variables.html

As long as your bucket is allowed to have multiple post-its (labels), and
as long as it's easy to unstick a post-it from one bucket and apply it to
another, there's no confusion.

The problem arises when one has only buckets with their individual names,
such as A and B, such that detaching the name (a post-it) from the bucket
can't happen.

How can changing the contents of bucket A change the contents of bucket B?
Answer:  they're the same bucket.  But if each bucket has a name carved
into it, as if in stone...

We see that mental picture in lots teaching materials, appropriate for
other languages more than for Python:

Example:

https://www.slideshare.net/PeterAndrews1/variables-13032998

(these slides don't suggest I can detach names from buckets and use them
for other buckets)

I would emphasize that Python buckets are essentially anonymous in the
sense that they have no permanent name assigned to them, and when no names
are assigned, they're eligible for garbage collection.

Getting clear on all this is not extraneous but essential as when we get to
pandas and start slicing in to larger data frames, we often get "views" not
"copies".  Python is just like that.

import pandas as pd
import numpy as np

data = pd.DataFrame(data = np.random.randint(9,size=9).reshape(3,3),
                    index = ['A','B','C'])

view = data.loc["A":"B"]  # slice (rows A,B), no copy made
print(data)
print()
view.loc['A'][0] = -1 # assign to slice, row A, column 0
print(data) # underlying DataFrame has changed

   0  1  2
A  3  8  5
B  6  4  1
C  7  4  4

   0  1  2
A -1  8  5
B  6  4  1
C  7  4  4

Kirby


PS:  I don't think it necessary to quote an entire thread one adding to it,
given the public archive contains the entire conversation.  I encourage
posters to trim, and keep only what they're directly responding to.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edu-sig/attachments/20180603/decaf2b6/attachment-0001.html>


More information about the Edu-sig mailing list