Modifications

Sauter à la navigation Sauter à la recherche
1 057 octets ajoutés ,  10 novembre 2016 à 12:22
Ligne 63 : Ligne 63 :     
== Les paquets (Packages) ==
 
== Les paquets (Packages) ==
 +
Sometimes your code can get so complex that putting it into a single file doesn't make sense.  In these cases you can break code into multiple files and create a Python package that puts all the code together into what looks like a simple module your scripts can import.  MicroPython supports the concept of Python packages just like normal Python so you can better structure complex scripts.
 +
 +
First be sure to read the [https://docs.python.org/3/tutorial/modules.html#packages official documentation on Python packages]. Packages in MicroPython for the most part work just the same as in Python.
 +
 +
Now create a Python package on your computer by creating a directory called '''test'''.  Inside that directory create a file called '''add.py''' and place inside it the add function code:
 +
 +
<syntaxhighlight lang="python">
 +
def add(a, b):
 +
    return a + b
 +
</syntaxhighlight>
 +
 +
Create a file '''subtract.py''' in the same location and place in it the subtract function code:
 +
 +
<syntaxhighlight lang="python">
 +
def subtract(a, b):
 +
    return a - b
 +
</syntaxhighlight>
 +
 
{{traduction}}
 
{{traduction}}
   
{{FEATHER-CHARGER-MODULE-TRAILER}}
 
{{FEATHER-CHARGER-MODULE-TRAILER}}
29 918

modifications

Menu de navigation