Warning – these notes on Python are based on very short acquaintance
and may well be misleading and/or factually incorrect.
OK – coming late to the table most people would think – but then
again. The last time I took a look at Python was around the time Python 3 came
out and the status and functionality of the mass of C support libraries was in
some doubt. Turns out that is still true in many instances with a lot of effort
still seemingly going into Python 2.n and it is not clear that the world at
large has collectively taken the big step to Unicode.
Anyway, I wanted to do some stuff with one of those new
Raspberry Pi 2 machines and Python looks to be the lingua Franca for that platform
when running Linux. So I had another look. **
The specific feature that figures in any introduction to
Python is that white space (specifically line indentation) is significant and
part of the language syntax. Most introductions wax on about there being no
need for {curly braces} or semicolons but then again you do have to frown a bit
at those unmentioned colons at the end of significant lines
>>> def myfun(arg):
. . .
>>> for w in something:
. . .
Then my reading got to len(something).
What? I thought.
Surely the length of something (number of characters in a string or the count
of entries in a list) would be an attribute of the relevant object. I even
found Guido van Rossum’s explanation in an old Python FAQ but also found it
less than convincing. I looked further and found there were at least 60
language features implemented as methods. Wow! I thought this is BASIC born
again. There are even Input and Print methods. Then I looked at how such
methods as len() were implemented – and then I think I reached an understanding.
Each object type (tricky to get the terminology right) where
a length measurement makes sense implements a method called __len__() that will
be used automatically by the language len() method. Programmers can implement
the __len__() method on their custom objects confident that the language’s
len() method will act as a standard interface to that functionality. In a
language envisioned for code sharing (and multiple standard libraries) this is
actually a powerful concept. You don’t have to know how (say) an unknown object
implements a readable string representation of itself – you can just use
str(object) with confidence.
So it looks like the BASIC style language methods can be
extended to include new types of object which is a massive step forwards.
Python is also big on Lists. I suspect that languages influential
in the design of Python included Prolog, Smalltalk and Lisp (I might have said a
Lispy JavaScript but the languages while not quite contemporaneous were both
born around the same time). No surprise then that Python is a very popular language
in AI research. There is nothing in Python Lists that you can’t do with (say)
C# but the language is rich in list manipulation functionality “out of the box”.
I love the fact that you can use expressions to create and
populate a list:
>>> squares = [x**2 for x in range(10)]
>>> squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
And range() itself is a fascinating and flexible tool for
controlling loops.
Tuples look interesting as immutable collections of heterogeneous
elements although I have yet to work out how a mutable element (like a list)
might fare inside a tuple.
Sets are another specialised form of list with support for
set operators like union and intersection. Takes me back to college maths. Can’t
wait to see what I can do with these. Curley braces do turn up in the language here and then also with dictionaries.
Dictionaries are available and look well supported and I
have always liked the “associative array” concept
Functions are first class objects and I greeted the huge
range of options for passing arguments into a function with a lot of interest.
There are potential traps for the unwary here I think but also a flexibility
not available anywhere else. Lambda’s as well of course.
Classes have similarities to the equivalent JavaScript
objects. They can contain the equivalent of static variables, instance
variables, attributes and methods. Instance variables can be created and
manipulated on individual instances of a class. Scope and the order that
objects are defined or assigned can impact upon the meaning of the code.
Classes support inheritance (including multiple inheritance)
with the ability to override base class methods. Operator overloading is also
supported.
The construction and importing of code modules (which then
act as name spaces) is an effective mechanism for code structure and code
sharing.
Python runs into a bit of a steep incline (not a wall) when
it comes to complex interactions with the user. It is fundamentally a scripting
language and is not primarily intended to maintain a GUI (Graphical User
Interface). KiVy is going to be bigger on the Raspberry Pi with the newly
announced touch display. For PCs Iron Python is an open source community
project that integrates the Python language with .NET might well be one
workaround for more complex projects and with Visual Studio 2015 (community
edition) being free for a lot of projects and developers it is a viable option.
When you have been working in JavaScript for a few days and the
code is really starting to flow then this is partly because you have erected a
good mental model of the variable and method scope in play. I strongly suspect
that constructing and maintaining a similar mental model for a large Python
codebase would be essential – there are some similarities but also some key
differences. Getting “in the groove” might take a little effort.
None of this is intended to act as any sort of Python
introduction – rather the intention is to communicate a little of the
enthusiasm I am feeling for the language now I have taken a proper look. Python
is different (a big bonus), clever and very capable. Now all I have to do is
learn how to write effective code in this language – a big step on from just
assembling an overview of the functionality after all. So the next side-project
is going to be in Python.
** plan is to have two SD cards so I can “dual boot” one of
the Linux variants available for the Pi and Windows 10 – should be fun. I also
see Python runs on Windows OIT Core as well as on the usual Linux variations.