2016-05-06 1 views

Répondre

5

C'est ce que functools.partial pourrait aider à:

from functools import partial 

foo2 = partial(foo, my_string="my_string_example") 

Démo:

>>> from functools import partial 
>>> def foo(my_num, my_string): 
...  print(my_num, my_string) 
... 
>>> foo2 = partial(foo, my_string="my_string_example") 
>>> foo2(10) 
(10, 'my_string_example') 
>>> foo2(30) 
(30, 'my_string_example') 
+0

Exactement ce dont j'avais besoin. Merci! –

0

Cela devrait le faire:

>>> def foo(a, b): 
... return a+b 
... 
>>> def makefoo2(a): 
... def f(b): 
...  return foo(a,b) 
... return f 
... 
>>> foo2 = makefoo2(3) 
>>> foo2(1) 
4 

Évidemment, vous pouvez modifier la définition de foo au goût .