je veux exécuter une fonction avec les paramètres que j'ai stocké dans un dico.
Comment je fais?
Pour l'instant j'ai crée une chaine avec ce dico et j'ai fait exec(str(maFunc)+'('+params+')')
Il y a probablement plus joli non ?
Version imprimable
je veux exécuter une fonction avec les paramètres que j'ai stocké dans un dico.
Comment je fais?
Pour l'instant j'ai crée une chaine avec ce dico et j'ai fait exec(str(maFunc)+'('+params+')')
Il y a probablement plus joli non ?
utilise maFonction(**dico), et on utilisera maFonction(*liste) avec une liste d'arguments.Citation:
Envoyé par GConstant
le_GLuCode:
1
2
3
4
5
6
7
8
9
10
11 >>> dico = { 'spam':'toto', 'eggs':2 } >>> def maFonction(spam=None, eggs=0): ... print 'spam:', spam, "eggs:", eggs ... >>> maFonction() spam: None eggs: 0 >>> maFonction(dico) spam: {'eggs': 2, 'spam': 'toto'} eggs: 0 >>> maFonction(**dico) spam: toto eggs: 2 >>>