Commit 4e4f1026 authored by Thibaut.Lunet's avatar Thibaut.Lunet

TL: correction for slide on functions

parent 7b1830bd
......@@ -60,7 +60,7 @@ elif 1 == 0: # Not mandatory
print('Toujours pas')
else: # Not mandatory
print("OK d'accord")
# For loop
for i in range(5):
print('i = {}'.format(i))
......@@ -76,25 +76,21 @@ while i < 10:
# %% Slide : Function definition
def funcA(a, b=1):
return a + b
# funcA(0.5, 2) = funcA(0.5, b=2) = 2.5
# funcA(1) = 2
# funcA() -> ERROR
def funcB(*args, **kwargs):
# args = list of argument = list
for elt in args:
print(elt)
# kwargs = list of keyword arguments = dict
for key in kwargs.keys():
print('{}-{}'.format(key, kwargs[key]))
# Try: funcB(1, 2, 3, 5, b=10, c=45), funcB(1, 2, a=10, bb=45), ...
# WARNING: funcB(1, 2, b=10, c=45, 5) => ERROR
# Example of kwargs use
kwargs = {'b': 12}
print(funcA(1, **kwargs)) # Returns 13
kwargs['b'] = 0
print(funcA(1, **kwargs)) # Returns 1
print(a + b)
funcA(1) # Print 2, default value (1) used for b
funcA(0.5, 2) # Print 2.5, 2 is used for b
funcA(0.5, b=2) # Equivalent way to set b=2
# funcA() -> ERROR : at least a must be given
# Arguments can be passed as dictionnary
def funcB(x, y, p1=None, p2=1, p3='o', p4=False):
print('{}, {} -- p1={}, p2={}, p3={}, p4={}'.format(x, y, p1, p2, p3, p4))
# -- Arguments are only written once
kwargs = {'p1': 12, 'p2': 2, 'p3': 'i', 'p4': True}
funcB(1, 2, **kwargs) # Print 1, 2 -- p1=12, p2=2, p3=i, p4=True
funcB('a', 'b', **kwargs) # Print a, b -- p1=12, p2=2, p3=i, p4=True
presentation/img/code-func.png

49.8 KB | W: | H:

presentation/img/code-func.png

45 KB | W: | H:

presentation/img/code-func.png
presentation/img/code-func.png
presentation/img/code-func.png
presentation/img/code-func.png
  • 2-up
  • Swipe
  • Onion skin
No preview for this file type
......@@ -259,7 +259,7 @@
%}
\begin{frame}{Function definition}
%\pythonCode
\includegraphics[width=0.9\linewidth]{code-func}
\includegraphics[width=\linewidth]{code-func}
\end{frame}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment