MICROPYTHON WITH THE ESP32 GUIDE SERIES

MicroPython Vs CPython

In this lesson, I will discuss some of the differences between MicroPython and CPython that I believe are most important to know when you are getting started with MicroPython.

As you know by now, MicroPython and CPython are two different programming languages. MicroPython has copied CPython as faithfully as possible to create a high-level language programming experience for microcontrollers. There are differences between the two languages, which I would like to summarize in the next few minutes.

See the MicroPython documentation for detailed differences between MicroPython and CPython

The differences between MicroPython and Python are documented in detail on the MicroPython website. There, you can find a complete list of those differences and code examples that demonstrate them. In this lesson, I will discuss some of the differences between MicroPython and CPython that I believe are most important to know when you are getting started with MicroPython.

Syntax

Let's begin with syntax. 

In CPython, you can do things like forget to put a space between a literal number and a keyword to form an expression, and that will be okay. CPython has enough flexibility to forgive mistakes like this. The same error in MicroPython, however, will generate a syntax error. MicroPython developers had to throw away the logic needed to deal with typos like that to fit the limited storage of the target devices.

Functions and "self"

Another example of these differences between the two languages is how the "self" keyword is handled. When self is used in a function in CPython, it does not count as an additional argument, but MicroPython does. As a result, if you provide an incorrect number of arguments to a function that contains the self keyword, then the error message it will get in CPython will be different from what you'll get in MicroPython.

I've got an example here. 

As you can see in this example, I'm calling the same function calculator in CPython and Python and I'm passing a single argument. I should have passed two arguments, but I made a mistake. I just passed a single argument here. As you  can see, I'm calling the same function with the same parameter. But the messages that are coming back to indicate the error are different. CPython is telling me that I've got one required argument missing. Where uPython is telling me that in total, there are supposed to be three arguments. I've only given two when, in fact, I've given one.

You can see that the message there is coming through here in MicroPython is or can be a bit confusing, which can throw you off and cause you to rely on your program's debugging. But as long as you are aware of the situation with the self keyword, I think you'll be able to get past issues like this.

Number formatting

Here's another subtle difference that has to do with formatting in this particular example of a floating-point number. 

When you print out formatted floating-point numbers, the result may differ between MicroPython and CPython. Here, I'm printing out this floating number, using the same commands between CPython and C Python - UPython being MicroPython. And as you can see that what comes out is different on each occasion. In this case, when used with CPython, the “g” operator applies the exponential format to a number. In the output, the “g” operator differs between the two implementations.

Strings

Following is an  example of differences relating  to the string. As you may know, CPython contains powerful string manipulation functions, and not all of them are available on MicroPython. Two examples: “start with” and “end with”. These allow you to check if a string starts or ends with a specific character or string of characters.

In MicroPython, these functions only work in their basic format without the start and end index parameters. In the example here in this slide, you can see that the call to the end with function fails when we use it with the three parameters but work with a single parameter. In CPython, no problem. Either one will work correctly and as expected.

JSON

Next up, we've got JSON.

In this guide and course, we'll use the MicroPython JSON module to work with Internet of Things services. Unlike CPython and the CPython version of the JSON module, uJSON does not throw an exception if an object is not serializable.

And this means that if your program receives a JSON document from a web service that is not valid, you will not be able to deal with it gracefully using an exception handler. You will have to deal with this manually, or your program will crash.

Conclusion

All right, so these were just some of the subtle differences between CPython and the MicroPython implementation. And, of course, there are many more. Again, the best place to learn about them and keep track of the changes is the MicroPython documentation.

In the following lesson, I'll show you some of the best online resources for MicroPython. And these are the resources that you'll want to bookmark so that you can access them easily any time you work with MicroPython.

Learn MicroPython for the ESP32

With this video course, you will learn how to use the
MicroPython programming language with the ESP32 micro-controller.


MicroPython is the perfect language for anyone looking for the easiest (yet still powerful) way to program a micro-controller.

Last Updated 9 months ago.

We publish fresh content each week. Read how-to's on Arduino, ESP32, KiCad, Node-RED, drones and more. Listen to interviews. Learn about new tech with our comprehensive reviews. Get discount offers for our courses and books. Interact with our community. One email per week, no spam; unsubscribe at any time

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}