【Hardcore Real Stuff】MicroPython vs CPython: Positioning, Syntax & Hardware Comparison
This article introduces MicroPython, its distinctions from CPython, supported hardware platforms, .mpy precompiled bytecode, and the main differences in syntax,
【Preliminary Note】The original hardware example in this article was written based on the RP2040. The actual hardware used in this hands-on demonstration features the W55RP20 as the main controller chip. The circuit logic and UF2 flashing operation principles are universally applicable, with only the main controller model differing. The original chip model mentioned in the circuit descriptions below is provided for reference purposes only.
1.MicroPython Intro.
MicroPython provides the following core data types: str (including basic Unicode support), bytes , bytearray , tuple, list, dict, set, frozenset, array.array, collections.namedtuple, classes, and instances. Built-in modules include os, sys, time, re, and struct.It also supports the _thread module (multithreading), socket sockets and ssl for networking, as well as asyncio coroutines.2.CPython和MicroPython的差异
MicroPython. Its core purpose is to help you quickly understand the differences between it and the CPython (the standard Python running on computers) that we commonly use — there's no need to get tangled up in complicated technical details. As long as you grasp the two key points of "version support scope" and "practical usage differences" first, you can avoid pitfalls when writing code later on.MicroPython 's " Python version basis": it does not support the latest Python version, but is implemented with Python 3.4 as the core, and additionally incorporates some selected features from Python 3.5 and later versions.For instance, it supports some features from versions 3.5,3.6 up to 3.10, but not all of them. The tutorial will clearly list the specific "optional features" it supports in the version order from 3.5 to 3.10, so you don't need to memorize all of them; it's enough to know that it is based on version 3.4 and comes with some features from higher versions.MicroPython implements a feature with the same name as CPython, it may behave differently in use. These differences mainly concentrate in 5 areas frequently used by beginners, which you will most likely encounter when writing code on a regular basis:Syntax): for example, literals (such as the notation of numbers and strings), operators (special operations beyond basic arithmetic like addition, subtraction, multiplication and division), Unicode character processing, as well as variable unpacking (operations like a, b =(1,2)), with some details differing from CPython;Core language): for example, the inheritance of class, the parameter handling of function, and the execution logic of generator — the actual behavior of these fundamental features may differ from what you are accustomed to in CPython;Runtime): For example, commonly used features like f-strings (formatted strings, such as f"hello {name}"), and the rules for import ing modules — these high-frequency features you use in daily coding may have special restrictions or usage in MicroPython;Builtin types): Types that you use every day such as dictionaries (dict), lists (list), strings (str), and integers (int) — some of their methods or functions may differ from those of CPython — for example, certain advanced usages of dict and the encoding handling of str; error types like exceptions (Exception) and OSError may also have different trigger conditions and prompt messages;array module for array processing, the os module for operating system-related operations, the json module for JSON processing, and the random module for generating random numbers. The features in MicroPython are usually "stripped-down versions" — only core functions are retained, and some methods available in CPython are not present in MicroPython, or have different parameters.MicroPython is "incomplete"; rather, they are optimizations it makes to adapt to the "small memory, low performance" characteristics of embedded devices such as microcontrollers.