forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Add note about position-only arguments in CPython vs MicroPython.
Required modifying the gen-cpydiff.py code to allow a "preamble" section to be inserted at the top of any of the generated files. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <[email protected]>
- Loading branch information
1 parent
c8772b7
commit 307ecc5
Showing
2 changed files
with
43 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
.. Preamble section inserted into generated output | ||
|
||
Positional-only Parameters | ||
-------------------------- | ||
|
||
To save code size, many functions that accept keyword arguments in CPython only accept positional arguments in MicroPython. | ||
|
||
MicroPython marks positional-only parameters in the same way as CPython, by inserting a ``/`` to mark the end of the positional parameters. Any function whose signature ends in ``/`` takes *only* positional arguments. For more details, see `PEP 570 <https://peps.python.org/pep-0570/>`_. | ||
|
||
Example | ||
~~~~~~~ | ||
|
||
For example, in CPython 3.4 this is the signature of the constructor ``socket.socket``:: | ||
|
||
socket.socket(family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None) | ||
|
||
However, the signature documented in :func:`MicroPython<socket.socket>` is:: | ||
|
||
socket(af=AF_INET, type=SOCK_STREAM, proto=IPPROTO_TCP, /) | ||
|
||
The ``/`` at the end of the parameters indicates that they are all positional-only in MicroPython. The following code works in CPython but not in most MicroPython ports:: | ||
|
||
import socket | ||
s = socket.socket(type=socket.SOCK_DGRAM) | ||
|
||
MicroPython will raise an exception:: | ||
|
||
TypeError: function doesn't take keyword arguments | ||
|
||
The following code will work in both CPython and MicroPython:: | ||
|
||
import socket | ||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters