With Python now more popular than ever, thanks to its extensive standard library and its ability to support multiple programming paradigms, it's a wonder this discussion is even ongoing.
The fact that it is clues us into the idea that there must be something about Python and robotics that we're maybe not clear on.
Superprof now takes a look at what the issues are, as well as the advantages of at least partially coding your robot in Python.
Find good python classes here on Superprof.
Python has been around for 30 years but it was an idea several years in the making. It is based on ABC, a general-purpose programming language that originated at CWI, a theoretical computer science and mathematics research facility in the Netherlands.
Find good python tutorial here on Superprof.
Guido van Rossum is the original Pythonista; he's known as the Benevolent Dictator for Life even though he stepped back from being solely responsible for Python's evolution in 2018. In a whimsical turn, he named his language after Monty Python - not after the constrictors found in the tropics.
In keeping with the idea that Python should be fun to use, many of their references include playful names rather than enigmatic-sounding codes. As an example, instead of the common programming placeholders foo and bar, Python recognises spam and eggs - a running Monty Python gag.
Python isn't just fun, it's also cleaner and simpler to read and write. Unlike many computer languages, Python does not use curly brackets, nor does it demand a lot of exceptions. To make it easier to read, Python draws on whitespace indentation: an increase indicates a new block and a decrease in indentation signals a block's end.
This off-side rule, another name for this practice of indentation, makes the program's semantic structure visually recognisable. Note that other languages also indent their code but that formatting is semantically insignificant in most cases.
Programming languages are called languages for a reason.
Just like learning any other language - with its unique vocabulary, grammar rules and how words must be arranged to make sense, programming languages have rules that must be followed. Otherwise, the program won't work at all or, at best, might work but not as intended.
What if you could copy and paste already-written blocks of language where you needed them, much as a language learner might copy/paste a sentence they ran through translation software? At its core, that is Python's driving philosophy.
Rather than formulating a complex set of statements and expressions - van Rossum was frustrated at how many of those there already were, he purposefully meant for Python to have as small a core language as possible while simultaneously building an extensive standard library to complement it.
This ease of use, coupled with its fun approach to programming makes Python a favourite of novice developers. Its versatility and simple structure make web development one of the prime uses of this language.
Find good python courses London here on Superprof.
Python for Performance?
Python being used so extensively in web development should be a clue that it is not well-adapted to speed and performance.
Take a look at a robot - any robot, even the one in the picture above. Can you tell which language it's coded in? No, of course not! You're too busy being amazed at what it can do! But how does it do everything it can do?
With commands, of course. And the language those commands are written in dictates everything from the order of the processes to how often and how quickly they happen.
If you have any kind of background in robotics and programming, you're likely aware that C++ is the standard language for robotic programming. Still, nothing says you can't write aspects of the robots' programmes in Python with the bulk of it written in C++ or, if you have the time and your robot is not meant to dazzle with speed, you could write an entire Python program.
If you do, you'll be trading performance for the straightforward simplicity and ease of use that Python can afford you.
The thing is, regardless of the face and skin the robot wears and how sluggishly it appears to function, at the programming level, things have to happen in milliseconds.
Let's say you're writing in a control loop; a series of instructions that distil into an action or inaction the robot will take. Each loop will have to run at 100 milliseconds, often faster. Python's very nature dictates that it is not suited to swift operation. Indeed, depending on the hardware and software involved, Python can run up to 100 times slower than C++!
Remember that Python's code is primarily meant to automatically manage memory - something called garbage collection. Its purpose is to try and reclaim memory that was allocated by the program but is not reference at a particular point in the operation.
It runs well on top of or alongside a robot's program - and may even enhance the device's performance by continuously freeing up memory but, for functions requiring lightning-fast decisions, programming your robot in C++ would be far more efficient. Especially if you're talking about enterprise-level robots, such as robots used in manufacturing, where performance matters.
It's your turn to talk: is Python better suited to computer game programming than to robotics?
Why Not Just C++?
As any robot programmer can tell you, it is entirely possible to run whole robotic systems on just C++ but there are advantages to adding a Python binding.
Python is an interpretive language, meaning that it translates instructions within the program into machine code and then follows those instructions to execute that part of the program. The benefit is that you'll spend less time compiling code, which will allow for faster program testing and launching.
In fact. Python is a great way to test specific parts of your program. Let's say you're concerned about how your program will direct your robot's movement. A basic script in Python would be enough to test just that aspect of your robot's functions without having to scrub your code, line by line, looking for errors or faults.
Not every line of code represents a vital or critical robotic function. There are background processes that do not impact a robot's performance or safe operation; those could be written in Python. But for things like motion planning algorithms and control loops - both critical to your application, using Python is simply not the best way to go.
Experts in the field agree that there's no way around C++ as the main language in robotics for the foreseeable future but they also concede that Python has its uses in the field of robotics. Just not as many as in machine learning.
The Best Uses for Python in Robotics
By now, sitting with pieces of robot all around you, you might be wondering: where can I use Python?
If you're building a robot just for the fun and experience of it - or if you need to build a prototype as a proof of concept (POC) for your startup, you can write your entire code in Python.
On the other hand, if your boss or company is keeping you on a schedule and to a budget, and they've given you clear performance standards they expect their new robot to have, you might limit your use of Python to troubleshooting and testing your program.
It would be a good idea to bind a few Python modules to run the non-critical parts of your robot's program or program your graphic user interface (GUI) and application programming interface (API) in Python. For now, that's about the extent of it.
Does Python have a future in robotics beyond testing and overseeing non-critical functions?
The field of artificial intelligence (AI) is wide open and rapidly advancing; its possibilities are endless. Now, for the best news: Python is uniquely suited to AI. Go ahead, let your imagination run away with you...
- smart houses filled with robotic appliances, all running on Python
- robots as health companions - imagine if we had them in today's pandemic wards!
- virtual assistants: robots as chatbots, all the way up to ones that can make travel and hotel arrangements
- robots as space explorers would collect data without risking human lives
- public safety - have you seen New York City's robotic dog?
- robots in education: essay graders (robo-readers) would free teachers from the time-consuming task of reading and grading every student essay
Some of these applications already exist - surely you've accessed websites that had chatbots to guide you, right? Others sound a bit far-fetched. Remember, though, that it was just 70 years ago that Isaac Asimov compiled his I, Robot series and less than 20 since those stories were brought to film.
But where are we now? Self-driving cars are in the testing stages and several robotics companies offer robot companions for sale. Innovation is happening faster than perhaps we can keep up with... and Python is well placed to drive all of those user interfaces.
Now, discover how big a role Python plays in data science.
The platform that connects private tutors and students