rendez votre code python plus beau !
DESCRIPTION
Présentation à PyCon FR à Strabsourg le 25 octobre 2013.TRANSCRIPT
![Page 1: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/1.jpg)
Rendez votre code Python plus beau !
Ronan Amicel @amicel
PyCon FR – 25 octobre 2013 – Strasbourg
![Page 2: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/2.jpg)
Ronan Amicel
Founder @ PocketSensei !
Hacker in Residence @ TheFamily
![Page 3: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/3.jpg)
Version originale
Raymond Hettinger !
PyCon US 2013 !
Vidéo sur pyvideo.org
![Page 4: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/4.jpg)
Du code plus beau ?
![Page 5: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/5.jpg)
Du code plus beau ?
• plus simple
![Page 6: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/6.jpg)
Du code plus beau ?
• plus simple
• plus concis
![Page 7: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/7.jpg)
Du code plus beau ?
• plus simple
• plus concis
• plus clair
![Page 8: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/8.jpg)
Du code plus beau ?
• plus simple
• plus concis
• plus clair
• plus idiomatique (== plus « pythonique »)
![Page 9: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/9.jpg)
Du code plus beau ?
• plus simple
• plus concis
• plus clair
• plus idiomatique (== plus « pythonique »)
• plus performant
![Page 10: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/10.jpg)
Itérations
![Page 11: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/11.jpg)
Itérer sur un intervalle d’entiers
![Page 12: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/12.jpg)
Itérer sur un intervalle d’entiers
for i in [0, 1, 2, 3, 4, 5]:! print i**2
![Page 13: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/13.jpg)
Itérer sur un intervalle d’entiers
for i in range(6):! print i**2
for i in [0, 1, 2, 3, 4, 5]:! print i**2
![Page 14: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/14.jpg)
Itérer sur un intervalle d’entiers
for i in range(6):! print i**2
for i in [0, 1, 2, 3, 4, 5]:! print i**2
for i in xrange(6):! print i**2
![Page 15: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/15.jpg)
Itérer sur une collection d'objets
![Page 16: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/16.jpg)
Itérer sur une collection d'objets
plats = ['choucroute', 'munster', 'kouglof']
![Page 17: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/17.jpg)
Itérer sur une collection d'objets
plats = ['choucroute', 'munster', 'kouglof']
for i in range(len(plats)):! print plats[i]
![Page 18: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/18.jpg)
Itérer sur une collection d'objets
plats = ['choucroute', 'munster', 'kouglof']
for i in range(len(plats)):! print plats[i]
for plat in plats:! print plat
![Page 19: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/19.jpg)
Itérer en partant de la fin
![Page 20: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/20.jpg)
Itérer en partant de la fin
plats = ['choucroute', 'munster', 'kouglof']
![Page 21: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/21.jpg)
Itérer en partant de la fin
plats = ['choucroute', 'munster', 'kouglof']
for i in range(len(plats)-1, -1, -1):! print plats[i]
![Page 22: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/22.jpg)
Itérer en partant de la fin
plats = ['choucroute', 'munster', 'kouglof']
for i in range(len(plats)-1, -1, -1):! print plats[i]
for plat in reversed(plats):! print plat
![Page 23: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/23.jpg)
Itérer en maintenant un indice
![Page 24: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/24.jpg)
Itérer en maintenant un indice
plats = ['choucroute', 'munster', 'kouglof']
![Page 25: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/25.jpg)
Itérer en maintenant un indice
plats = ['choucroute', 'munster', 'kouglof']
for i in range(len(plats)):! print i, '-->', plats[i]
![Page 26: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/26.jpg)
Itérer en maintenant un indice
plats = ['choucroute', 'munster', 'kouglof']
for i in range(len(plats)):! print i, '-->', plats[i]
for i, plat in enumerate(plats):! print i, '-->', plat
![Page 27: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/27.jpg)
Itérer sur deux collections en même temps
![Page 28: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/28.jpg)
Itérer sur deux collections en même temps
vins = ['riesling', 'edelzwicker', 'gewurztraminer']!plats = ['choucroute', 'munster', 'kouglof']
![Page 29: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/29.jpg)
Itérer sur deux collections en même temps
vins = ['riesling', 'edelzwicker', 'gewurztraminer']!plats = ['choucroute', 'munster', 'kouglof']
n = min(len(vins), len(plats))!for i in range(n):! print vins[i], '-->', plats[i]
![Page 30: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/30.jpg)
Itérer sur deux collections en même temps
vins = ['riesling', 'edelzwicker', 'gewurztraminer']!plats = ['choucroute', 'munster', 'kouglof']
n = min(len(vins), len(plats))!for i in range(n):! print vins[i], '-->', plats[i]
for name, plat in zip(vins, plats):! print name, '-->', plat
![Page 31: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/31.jpg)
Itérer sur deux collections en même temps
vins = ['riesling', 'edelzwicker', 'gewurztraminer']!plats = ['choucroute', 'munster', 'kouglof']
n = min(len(vins), len(plats))!for i in range(n):! print vins[i], '-->', plats[i]
for name, plat in zip(vins, plats):! print name, '-->', plat
for name, plat in izip(vins, plats):! print name, '-->', plat
![Page 32: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/32.jpg)
Itérer sur deux collections en même temps
vins = ['riesling', 'edelzwicker', 'gewurztraminer']!plats = ['choucroute', 'munster', 'kouglof']
n = min(len(vins), len(plats))!for i in range(n):! print vins[i], '-->', plats[i]
for name, plat in zip(vins, plats):! print name, '-->', plat
for name, plat in izip(vins, plats):! print name, '-->', plat
from itertools import izip
![Page 33: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/33.jpg)
Itérer selon un ordre de tri
![Page 34: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/34.jpg)
Itérer selon un ordre de tri
plats = ['choucroute', 'munster', 'kouglof']
![Page 35: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/35.jpg)
Itérer selon un ordre de tri
plats = ['choucroute', 'munster', 'kouglof']
for plat in sorted(plats):! print plat
![Page 36: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/36.jpg)
Itérer selon un ordre de tri
plats = ['choucroute', 'munster', 'kouglof']
for plat in sorted(plats):! print plat
for plat in sorted(plats, reverse=True):! print plat
![Page 37: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/37.jpg)
Trier selon un critère particulier
![Page 38: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/38.jpg)
Trier selon un critère particulier
plats = ['choucroute', 'munster', 'kouglof']
![Page 39: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/39.jpg)
Trier selon un critère particulier
plats = ['choucroute', 'munster', 'kouglof']
def compare_length(c1, c2):! if len(c1) < len(c2):! return -1! if len(c1) > len(c2):! return 1! return 0!!print sorted(plats, cmp=compare_length)
![Page 40: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/40.jpg)
Trier selon un critère particulier
plats = ['choucroute', 'munster', 'kouglof']
def compare_length(c1, c2):! if len(c1) < len(c2):! return -1! if len(c1) > len(c2):! return 1! return 0!!print sorted(plats, cmp=compare_length)
print sorted(plats, key=len)
![Page 41: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/41.jpg)
Dictionnaires
![Page 42: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/42.jpg)
Itérer sur les clésd'un dictionnaire
![Page 43: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/43.jpg)
Itérer sur les clésd'un dictionnaire
d = {! 'gewurztraminer': ‘kouglof',! 'edelzwicker': ‘munster',! 'riesling': ‘choucroute’,!}
![Page 44: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/44.jpg)
Itérer sur les clésd'un dictionnaire
d = {! 'gewurztraminer': ‘kouglof',! 'edelzwicker': ‘munster',! 'riesling': ‘choucroute’,!}
for key in d:! print key
![Page 45: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/45.jpg)
Itérer sur les clésd'un dictionnaire
d = {! 'gewurztraminer': ‘kouglof',! 'edelzwicker': ‘munster',! 'riesling': ‘choucroute’,!}
for key in d:! print key
d = {key: d[key] for key in d if not key.startswith('r')}
![Page 46: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/46.jpg)
Itérer sur les clés et les valeurs d'un dictionnaire
![Page 47: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/47.jpg)
Itérer sur les clés et les valeurs d'un dictionnaire
for key in d:! print key, '-->', d[key]
![Page 48: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/48.jpg)
Itérer sur les clés et les valeurs d'un dictionnaire
for key in d:! print key, '-->', d[key]
for key, value in d.items():! print key, '-->', value
![Page 49: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/49.jpg)
Itérer sur les clés et les valeurs d'un dictionnaire
for key in d:! print key, '-->', d[key]
for key, value in d.items():! print key, '-->', value
for key, value in d.iteritems():! print key, '-->', value
![Page 50: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/50.jpg)
Construire un dictionnaire à partir de paires
![Page 51: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/51.jpg)
Construire un dictionnaire à partir de paires
vins = ['riesling', 'edelzwicker', 'gewurztraminer']!plats = ['choucroute', 'munster', 'kouglof']
![Page 52: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/52.jpg)
Construire un dictionnaire à partir de paires
vins = ['riesling', 'edelzwicker', 'gewurztraminer']!plats = ['choucroute', 'munster', 'kouglof']
d = dict(izip(vins, plats))!{'gewurztraminer': 'kouglof', 'edelzwicker': ‘munster',! 'riesling': 'choucroute'}
![Page 53: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/53.jpg)
Construire un dictionnaire à partir de paires
vins = ['riesling', 'edelzwicker', 'gewurztraminer']!plats = ['choucroute', 'munster', 'kouglof']
d = dict(izip(vins, plats))!{'gewurztraminer': 'kouglof', 'edelzwicker': ‘munster',! 'riesling': 'choucroute'}
d = dict(enumerate(vins))!{0: 'riesling', 1: 'edelzwicker', 2: 'gewurztraminer'}
![Page 54: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/54.jpg)
Compter avec des dictionnaires
![Page 55: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/55.jpg)
Compter avec des dictionnaires
plats = ['choucroute', 'munster', 'choucroute', ‘kouglof',! 'munster', 'choucroute']
![Page 56: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/56.jpg)
Compter avec des dictionnaires
plats = ['choucroute', 'munster', 'choucroute', ‘kouglof',! 'munster', 'choucroute']
d = {}!for plat in plats:! if plat not in d:! d[plat] = 0! d[plat] += 1!{'kouglof': 1, 'munster': 2, 'choucroute': 3}
![Page 57: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/57.jpg)
Compter avec des dictionnaires
plats = ['choucroute', 'munster', 'choucroute', ‘kouglof',! 'munster', 'choucroute']
d = {}!for plat in plats:! if plat not in d:! d[plat] = 0! d[plat] += 1!{'kouglof': 1, 'munster': 2, 'choucroute': 3}
d = defaultdict(int)!for plat in plats:! d[plat] += 1
![Page 58: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/58.jpg)
Clarifier
![Page 59: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/59.jpg)
Clarifier les appels de fonctions avec des arguments mots-clés
![Page 60: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/60.jpg)
search(‘#pyconfr', False, 20, True)
Clarifier les appels de fonctions avec des arguments mots-clés
![Page 61: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/61.jpg)
search(‘#pyconfr', False, 20, True)
search(‘#pyconfr', retweets=False, numtweets=20, popular=True)
Clarifier les appels de fonctions avec des arguments mots-clés
![Page 62: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/62.jpg)
Clarifier les valeurs de retour multiples avec des tuples nommés
![Page 63: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/63.jpg)
Clarifier les valeurs de retour multiples avec des tuples nommés
doctest.testmod()!(0, 4)
![Page 64: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/64.jpg)
Clarifier les valeurs de retour multiples avec des tuples nommés
doctest.testmod()!(0, 4)
doctest.testmod()!TestResults(failed=0, attempted=4)
![Page 65: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/65.jpg)
Clarifier les valeurs de retour multiples avec des tuples nommés
doctest.testmod()!(0, 4)
doctest.testmod()!TestResults(failed=0, attempted=4)
from collections import namedtuple!TestResults = namedtuple('TestResults', ['failed', 'attempted'])
![Page 66: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/66.jpg)
« Unpacking »de séquences
![Page 67: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/67.jpg)
« Unpacking »de séquences
p = 'Ronan', 'Amicel', 37, '@amicel'
![Page 68: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/68.jpg)
« Unpacking »de séquences
p = 'Ronan', 'Amicel', 37, '@amicel'
prenom = p[0]!nom = p[1]!age = p[2]!twitter = p[3]
![Page 69: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/69.jpg)
« Unpacking »de séquences
p = 'Ronan', 'Amicel', 37, '@amicel'
prenom = p[0]!nom = p[1]!age = p[2]!twitter = p[3]
prenom, nom, age, twitter = p
![Page 70: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/70.jpg)
Performance
![Page 71: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/71.jpg)
Concaténer des chaînes
![Page 72: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/72.jpg)
Concaténer des chaînes
vins = ['riesling', 'edelzwicker', 'gewurztraminer',! 'pinot noir', 'pinot gris', 'muscat', 'sylvaner']
![Page 73: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/73.jpg)
Concaténer des chaînes
vins = ['riesling', 'edelzwicker', 'gewurztraminer',! 'pinot noir', 'pinot gris', 'muscat', 'sylvaner']
s = vins[0]!for name in vins[1:]:! s += ', ' + name!print s
![Page 74: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/74.jpg)
Concaténer des chaînes
vins = ['riesling', 'edelzwicker', 'gewurztraminer',! 'pinot noir', 'pinot gris', 'muscat', 'sylvaner']
s = vins[0]!for name in vins[1:]:! s += ', ' + name!print s
print ', '.join(vins)
![Page 75: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/75.jpg)
Décorateurs etcontext managers
![Page 76: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/76.jpg)
Utiliser des décorateurs pour factoriser de la logique
![Page 77: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/77.jpg)
Utiliser des décorateurs pour factoriser de la logique
def web_lookup(url, saved={}):! if url in saved:! return saved[url]! page = urllib.urlopen(url).read()! saved[url] = page! return page
![Page 78: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/78.jpg)
Utiliser des décorateurs pour factoriser de la logique
def web_lookup(url, saved={}):! if url in saved:! return saved[url]! page = urllib.urlopen(url).read()! saved[url] = page! return page
@cache!def web_lookup(url):! return urllib.urlopen(url).read()
![Page 79: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/79.jpg)
Décorateur cache
def cache(func):! saved = {}!! @wraps(func)! def newfunc(*args):! if args in saved:! return newfunc(*args)! result = func(*args)! saved[args] = result! return result!! return newfunc
![Page 80: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/80.jpg)
Ouvrir et fermer des fichiers
![Page 81: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/81.jpg)
Ouvrir et fermer des fichiers
f = open('data.txt')!try:! data = f.read()!finally:! f.close()
![Page 82: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/82.jpg)
Ouvrir et fermer des fichiers
f = open('data.txt')!try:! data = f.read()!finally:! f.close()
with open('data.txt') as f:! data = f.read()
![Page 83: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/83.jpg)
Utiliser des locks (verrous)
![Page 84: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/84.jpg)
Utiliser des locks (verrous)
# Créer un lock!lock = threading.Lock()
![Page 85: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/85.jpg)
Utiliser des locks (verrous)
# Créer un lock!lock = threading.Lock()
# Utiliser un lock (ancienne méthode)!lock.acquire()!try:! print 'Critical section 1'! print 'Critical section 2'!finally:! lock.release()
![Page 86: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/86.jpg)
Utiliser des locks (verrous)
# Créer un lock!lock = threading.Lock()
# Utiliser un lock (ancienne méthode)!lock.acquire()!try:! print 'Critical section 1'! print 'Critical section 2'!finally:! lock.release()
# Utiliser un lock (nouvelle méthode)!with lock:! print 'Critical section 1'! print 'Critical section 2'
![Page 87: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/87.jpg)
« One liners »
![Page 88: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/88.jpg)
Listes en compréhension et expressions génératrices
![Page 89: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/89.jpg)
Listes en compréhension et expressions génératrices
result = []!for i in range(10):! s = i ** 2! result.append(s)!print sum(result)
![Page 90: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/90.jpg)
Listes en compréhension et expressions génératrices
result = []!for i in range(10):! s = i ** 2! result.append(s)!print sum(result)
print sum([i**2 for i in xrange(10)])
![Page 91: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/91.jpg)
Listes en compréhension et expressions génératrices
result = []!for i in range(10):! s = i ** 2! result.append(s)!print sum(result)
print sum([i**2 for i in xrange(10)])
print sum(i**2 for i in xrange(10))
![Page 92: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/92.jpg)
Questions ?
![Page 93: Rendez votre code Python plus beau !](https://reader035.vdocuments.pub/reader035/viewer/2022081401/556a6120d8b42a7a138b53fa/html5/thumbnails/93.jpg)
Merci !