{{tag>Brouillon Python}} = Notes Python syntaxe == Liste de tous les mots clefs Python #help("keywords") import keyword keyword.kwlist ['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield'] keyword.softkwlist ['_', 'case', 'match'] == Fonctions Annotations de fonctions def func(a: int, b: int, c: int) -> int: return a + b + c === Liste des fonctions natives https://docs.python.org/fr/3/library/functions.html == Syntaxe kw = dict( changed=changed, query=cursor.query, statusmessage=statusmessage, query_result=query_result, rowcount=rowcount if rowcount >= 0 else 0, ) In [7]: rowcount=-1 In [8]: print(rowcount) if rowcount >= 0 else 0 Out[8]: 0 i: int = 10 s: str = 'string' == Lambda # Simple quote a string q = lambda s: f"'{s}'" # Double quote a string qq = lambda s: f'"{s}"' == Cast String to bool tls_verify = environ.get("VERIFY_SSL") # String to bool tls_verify = tls_verify.lower in ("true",) ou encore def str2bool(v): return v.lower() in ("yes", "true", "t", "1") == Décorateur Exemple def decorteur_print(_): def print_tmp(*args, **kv): print("Function print called") print(*args, **kv) return print_tmp @decorteur_print def print2(*args, **kv): print(*args, **kv) print2("Foo", "Bar", sep=";") Surcharger une fonction # On copie notre fonction print_bak = print def decorteur_print(_): def print_tmp(*args, **kv): print_bak("Function print called") print_bak(*args, **kv) return print_tmp @decorteur_print def print(*args, **kv): print_bak(*args, **kv) print("Foo", "Bar", sep=";") == Générateurs Voir : * https://zestedesavoir.com/articles/152/la-puissance-cachee-des-coroutines/ === Yield vs yield from ==== The yield statement The generator is a function that returns an iterator. Normally a generator function has a yield statement. To define a generator function we need to use a yield statement as shown below: def generate_number(): for x in range(10): yield x The generate_number has a yield statement, so it is a generator function. Let’s print the values from the generator : gen = generate_number() for number in gen: print(number) 0 1 2 3 4 5 6 7 8 9 ==== The “yield from” statement The “yield from” allows us to yield from another generator function or iterables. Let’s understand with some examples. Consider the following generator function : def gen_number(): yield 1 yield 2 yield 3 We can print the generator values using a loop. for num in gen_number(): print(num) 1 2 3 Let’s consider another function(gen_cool_numbers) using the gen_number generator function. def gen_cool_numbers(): yield 1000 for num in gen_number(): yield num yield 2000 for x in gen_cool_numbers(): print(x) 1000 1 2 3 2000 We can update the gen_cool_numbers function by replacing the for loop with yield from as shown below: def gen_cool_numbers(): yield 1000 yield from gen_number() yield 2000 for x in gen_cool_numbers(): print(x) 1000 1 2 3 2000 Yield from can also be used with iterable(list, tuple, string etc def gen_cool_numbers(): yield 1000 yield from [50,60,80] yield 2000 for x in gen_cool_numbers(): print(x) 1000 50 60 80 2000 Source : https://nolowiz.com/difference-between-yield-and-yield-from-in-python/