Skip to content

Grokking Python

One of the challenges – and major benefits – to an old school programmer learning Python is to get a handle on new ways to approach and talk about old problems. Java is ‘object oriented’ but much of how it does things is not that much different from the classic procedural languages. Python is another story.

In Python, everything is an object. That means there are no primitives.All variables are pointers to objects that have a number of properties like value and type. Pointers have a long history in C and they can be a source of confusion in some circumstances of you are not careful. Those properties that are available in Python are necessary to help keep things straight.

Constants are called immutable and an immutable list (list is the name used for an array) is called a tuple. Whereas a list with values that can be changed is enclosed in brackets, a tuple is optionally enclosed in parentheses. 

There are other collections like dictionaries and sets and there is functionality that allows common actions on these collections as well as handling set of collections. This is much more interesting than just having multi-dimensional arrays and hash indexing.

The Python iterator concept takes the old idea of a counter to step through a loop to a new level. Instead of just a counter variable that steps from here to there by how-much, an iterator is an object that will return successive values and, as an object, it can be controlled by mapping or filtering to produce interesting results.

Inline functions are established by using the key word “lambda” rather than the “def” that defines the standard function. There are some implications beyond this simple distinction that need exploration.

The use of white space seems to cause a lot of dissonance. That means arguments much like C programmers get into about line indentation. I don’t know why it bugs people as white space even goes back to Fortran on punch cards. Python just expands it a bit. Enforcing white space means that a lot of punctuation to define code blocks and statements isn’t necessary in Python and that can reduce errors and improve readability.

MicroPython is an effort to get the core ideas of Python development in a microcontroller. That means a modern interpreted coding environment to compete with BASIC, Forth, and similar classic options. For Python, it means trimming down the standard library and limiting the core language somewhat. 

Python has a lot of capabilities that could be useful in GPS applications. GPS data often comes in as records that are string of bytes that can be ASCII using standard NMEA clauses or binary using proprietary structures. Python provides a Struct function that will unpack these records by mapping to a format that indicates the binary data type and the manner the binary data is mapped to various data types. 

That is some of the base that everything else is built upon. What Python advocates refer to as “batteries included” is another level. If you want to anything in software, the odds are pretty good that someone has done it before. Standard language libraries are a result. Python has a very rich set of libraries that is included with its interpreter and is available anywhere Python is. Java also has an extensive library. C, in contrast, has a small default library but that is appropriate for its focus.

It’s all about paradigms for thinking about problems and expressing ideas. It used to be learning a new language was just about English or French or Russian. Programming languages are nowhere near as complex but can still provide a lot of insight into different ways to see things.