(Source: Quality Stock Arts - stock.adobe.com)
One of the most appealing aspects of programming in MicroPython is its similarity to the widely popular CPython used on desktop environments for almost two decades. Thanks to the nearly identical syntax and design paradigms, moving between the embedded and desktop development environments can be a virtually seamless experience. This can be highly desirable in the Internet of Things era, where data from embedded electronics products must be accessible on a plethora of platforms, including mobile, desktop, and the cloud. Development staff can be kept lean, and product development cycles tightened if the variation in tools and programming languages are minimized. That said, the very nature of the embedded hardware environment, as compared to desktop computing, necessitates some not insignificant differences between MicroPython and CPython.
The embedded electronics ecosystem is constrained in many ways that desktops or servers are not. First and foremost, they are constrained by energy consumption. Many embedded devices are battery-powered, and thus extending battery life is limited in terms of performance. Desktop computers may operate at several gigahertz, whereas microcontrollers are, at best, operating at speeds measured in the tens of megahertz. Constrained memory and storage also tend to be limiting factors that significantly impact the difference between CPython and MicroPython. With memory measured in mere kilobytes or megabytes, memory-intensive features are often reduced or even completely removed from the MicroPython implementation. Knowing the differences is important for developers who wish to avoid many frustrating hours debugging their embedded code. Here are the crucial differences developers ought to be aware of:
gc.collect()
:=
__new__
__del__
__doc__
sys.stdin
sys.stdout
sys.stderr
environ
getenv()
putenv()
unsetenv()
print()
struct
struct.pack(format, v1, v2)
import array as array
a = array.array('i', [1, 2, 3, 4])
print(1 in a)
CPython would print false, whereas MicroPython would return a not implemented error message.
false
MicroPython is a powerful yet easy-to-use programming language for developing embedded applications. However, it is limited when compared to the more feature-rich Python implementations that are used for creating desktop and cloud-native applications. Some differences may be remedied in future versions of MicroPython, or new differences may emerge as new features are added to the reference implementation of Python. To check out the latest differences between CPython and MicroPython, be sure to check out MicroPython's official documentation on their website.
Michael Parks, P.E. is the co-founder of Green Shoe Garage, a custom electronics design studio and embedded security research firm located in Western Maryland. He produces the Gears of Resistance Podcast to help raise public awareness of technical and scientific matters. Michael is also a licensed Professional Engineer in the state of Maryland and holds a Master’s degree in systems engineering from Johns Hopkins University.