LEARNING PYTHON AS AN R PROGRAMMER: Strings and Immutable Objects (and ChatGPT Win 1)

I first encountered the programming idea of immutable objects in .NET meetings conducted by the local Triangle .NET User Group years ago. I never fully understood the concept. This post communicates progress in that understanding while exploring differences and similarities in how Python and R use strings. A ChatGPT usage success (“win”) is also communicated.

Python – cannot change string object by index

Google’s Crash Course on Python (Coursera) featured the following in Week 4’s “Creating New Strings” lesson. One cannot change particular characters in a string object by index, unlike in R. Python springs “TypeError: ‘str’ object does not support item assignment”.

Google’s Crash Course on Python (Coursera) is taught by Site Reliability Engineer, Christine Rafla [https://www.linkedin.com/in/christinerafla/].

R – can change string object by index

On the other hand, we can assign a letter by index using R’s substr() function. substr(message, 3, 3) = "l" makes the third character of the message object a lower case L.

Immutable Objects – R and Python

I read some of the famous Hadley Wickham’s 2010 “Mutable Objects in R” to help me understand the computer programming paradigm of mutability. Wickham’s abstract communicated his creation of a package, mutatr, to support mutable objects, implying that R uses immutable objects normally. Other sources also supported that R generally uses immutable objects.

Code that uses immutable objects tends to be easier to reason about because effects are local. This is a big advantage: it is easier to understand how code works […] The disadvantage of immutability is its lack of modularity: since we can not modify objects directly, each function needs to return a copy of all objects that it modifies.

— Hadley Wickham

R quietly copies the object into RAM, and then changes the copy, when functions like substr() are used so it only appears to be using mutable objects. One can use tracemem() to see that the object name now refers to a different location in memory (RAM).

I asked ChatGPT to help me with the following quote from “Mutable Objects in R“, particularly with understanding the relationship between mutability and functional code.

“[authors van Roy and Haridi] suggest to keep as much code as possible functional, and only use mutable state when absolutely necessary, and when it is necessary, it is advantageous to ‘write stateful components so that they behave declaratively'”

So “functional” is distinguished from “mutable” in a programming context. I have most frequently encountered R referenced as a functional language, as Hadley Wickham did (“R, at its heart, is a functional language”), in his Advanced R book. And ChatGPT also indicated character objects (like message in the example above) were immutable. Credit my ChatGPT use for a win (see my fail in this previous post).

R and Python use immutable objects (R has a few exceptions). Mutability must have nothing to do with the ability to use substr() to change what appears to be the same message.

By the way, as Crash Course on Python instructs, we can use a Python technique called string slicing to create a new object with the same name but a different string value.

Finally, the below from “Mutable Objects in R” caused me to ask on Stack Overflow if Python is best understood as using Message Passing or Generic Functional object-oriented syntax. I currently await an answer.

Red text is Rick Pack’s.

Strings and IN operators – R and Python

Finally, referencing another example from Crash Course on Python, both R and Python have IN operators. Both use a lower-case in and only R surrounds those letters with percentage marks (%).

Leave a comment