Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente Prochaine révisionLes deux révisions suivantes |
python:bases:fonction [2022/09/03 10:18] – phil | python:bases:fonction [2022/10/28 12:09] – [Pour aller plus loin] phil |
---|
===== Python - Les fonctions ===== | ===== Python - Les fonctions ===== |
| |
[Mise à jour le : 14/6/2022] | [Mise à jour le : 28/10/2022] |
| |
* **Sources** | * **Sources** |
| |
* **Ressources** | * **Ressources** |
| * <html><a href="https://www.pythonmorsels.com/built-in-functions-in-python/" target="_blank"><strong>Fonctions intégrées Python à connaître</strong></a></html> |
* **Real Python** : <html><a href="https://realpython.com/python-main-function/" target="_blank">Defining Main Functions in Python</a></html> | * **Real Python** : <html><a href="https://realpython.com/python-main-function/" target="_blank">Defining Main Functions in Python</a></html> |
| |
Python ne disposant pas d'accolade pour délimiter les blocs de code, il existe une instruction **pass**, qui ne fait rien. | Python ne disposant pas d'accolade pour délimiter les blocs de code, il existe une instruction **pass**, qui ne fait rien. |
| |
<note tip>**pass** permet de définir une **fonction vide**. </note> | <callout type="tip" icon="true">**pass** permet de définir une **fonction vide**.</callout> |
| |
//Exemple// | //Exemple// |
{{ :python:docstring.png?nolink&500 |}} | {{ :python:docstring.png?nolink&500 |}} |
| |
<note tip>Les arguments peuvent être passés dans un** ordre quelconque** si on précise le nom des paramètres.</note> | <callout type="tip" icon="true">Les arguments peuvent être passés dans un** ordre quelconque** si on précise le nom des paramètres.</callout> |
| |
// Exemple// | // Exemple// |
</code> | </code> |
| |
<note warning>Ne **pas** utiliser **d'objet mutable** pour les valeurs par défaut. A <html><a href="https://docs.python.org/3/faq/programming.html#why-are-default-values-shared-between-objects" target="_blank">voir.</a></html></note> | <callout icon="fa fa-hand-stop-o" color="red" title="ATTENTION">Ne **pas** utiliser **d'objet mutable** pour les valeurs par défaut. A <html><a href="https://docs.python.org/3/faq/programming.html#why-are-default-values-shared-between-objects" target="_blank">voir.</a></html></callout> |
| |
//Exemple// | //Exemple// |
</code> | </code> |
| |
<note warning>Les fonctions lambda ne peuvent ni contenir des commandes ni contenir plus d’une expression. </note> | <callout icon="fa fa-hand-stop-o" color="red" title="ATTENTION">Les fonctions lambda ne peuvent ni contenir des commandes ni contenir plus d’une expression. </callout> |
| |
==== 5. Les fonctions natives (buit-in) ==== | ==== 5. Les fonctions natives (buit-in) ==== |
</code> | </code> |
| |
<note important>Un module **<nowiki>__name__</nowiki>** est égal à '**<nowiki>__main__</nowiki>**' lorsqu'il est lu à partir d'une entrée standard, d'un script ou d'une invite interactive.</note> | <callout type="warning" icon="true">Un module **<nowiki>__name__</nowiki>** est égal à '**<nowiki>__main__</nowiki>**' lorsqu'il est lu à partir d'une entrée standard, d'un script ou d'une invite interactive.</callout> |
| |
* **//Bonnes pratiques//** | * **//Bonnes pratiques//** |
* <html><a href="https://realpython.com/python-lambda/" target="_blank">How to Use Python Lambda Functions</a></html> | * <html><a href="https://realpython.com/python-lambda/" target="_blank">How to Use Python Lambda Functions</a></html> |
* <html><a href="https://realpython.com/run-python-scripts/" target="_blank">How to Run Your Python Scripts</a></html> | * <html><a href="https://realpython.com/run-python-scripts/" target="_blank">How to Run Your Python Scripts</a></html> |
| * <html><a href="https://realpython.com/python-sum-function/" target="_blank">Python's sum(): The Pythonic Way to Sum Values</a></html> |
| * <html><a href="https://realpython.com/len-python-function/" target="_blank">Using the len() Function in Python</a></html> |
| * <html><a href="https://realpython.com/python-min-and-max/" target="_blank">Python's min() and max(): Find Smallest and Largest Values</a></html> |
| * <html><a href="https://realpython.com/python-range/" target="_blank">The Python range() Function (Guide)</a></html> |
| |
---- | ---- |