[Tutor] Python to non-Python

ThreeBlindQuarks threesomequarks at proton.me
Sat Apr 15 20:44:36 EDT 2023


I recently was asked to take some Python Code snippets and rewrite it in R. This was code I had not written, and it made me think about the process including from the perspective of some of the questions asked here.

Questions often are asked why Python does not do something that some language they learned earlier or know about. I learned both R and Python fairly late after many other languages. So I am fairly comfortable with having many different approaches to the same kinds of problems both across languages and within one. Many are not flexible and one way to learn to be is to become multilingual in a computer language sense.

So what interested me here, as I see both languages as valid but with different and overlapping abilities, is what was an issue for me in the task.

Without explicit details, one barrier was the use of a common technique in Python:

first, second, third, last = f(args)

Would you believe quite a few other languages insist on returning only one thing with no unpacking easily available?

In this case, I returned a named list and the returned function value had to be saved as something like "values" and then examined so to get "second" you had to use a notation like values[[2]] or values$second or other such forms. So one lesson if writing something in python that may need to be copied into another language is perhaps not to overuse the ability to return multiple things. The feature is great but not easily portable.

One other lesson to mention is that the code used functions from numpy and pandas that I considered silly. The data it was operating on was not vectorized and was just individual values. The Python interpreter was happy to work on a scalar or with the vectorized data structures used by numpy or pandas of any length BUT when I hit an IF statement checking if it was non-null, that is not vectorized and blows up for anything more than length one. I had no comments in the code and initially assumed the data was a vector of many rows or something until that IF indicated otherwise.

In R, for what it is worth, everything is a vector, and the operations they borrowed from numpy and pandas are built-in. Still, without info on what the code actually did (which I figured out on my own) translation was harder than it needed to be.

The other thing I realized was about myself and this sort of applies to both Python and R.

I do not like visible loops much and detest complex deeply nested loops. There is nothing wrong with them, and little programming can be done without looping, but modern languages often help you hide many loops.

So when I rewrote the code, I found myself looking for ways to sort of simplify. I took a big chunk of code that was an IF then and ELIF and another few ELIF followed by an ELSE (upper case is for emphasis, not the actual code written) and used a package in R that allowed me to do something more like this:

keywords
CASE condition ~ return value,
CASE condition ~ return value,
...
CASE condition ~ return value,

default ~ return value

Basically the above sets the value of a variable by trying the first case and if the condition works, exits the whole thing with a value. If the first condition fails, it does the same with the second and so on, till it hits the default.

That is a sort of hidden loop that is roughly equivalent but in some sense easier for me to read and reason with. Python has some new features along similar lines. There are many such approaches, including some kinds of functional programing and recursion.

As for the nested loops, I moved some into functions called from the above so the overall outline is simpler.And, I rearranged the order and logic in places such as doing the more common things first. Enough examples!

Then I asked myself what someone might do if asked to change my R code back into similar Python.

I think sometimes a good teaching tool for learning a language can be taking one method of solving a problem then ask them to solve the same problem using a different method in the same language or simply to take a working program from another language or pseudocode. A real eye opener for me in python was how some loop methods could be written with some syntactic sugar as list comprehensions. Again, nice feature but it takes a little work doing the same thing in most other languages. Not much but ...

Sent with [Proton Mail](https://proton.me/) secure email.


More information about the Tutor mailing list