Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

I have various functions with a mix of positional and watchword contentions, and I might want to tie one of their arguments to a given worth (which is known simply after the function definition). Is there an overall method of doing that? 

Look at my first try:

def f(a,b,c): print a,b,c

def _bind(f, a): return lambda b,c: f(a,b,c)

bound_f = bind(f, 1)

Nonetheless, for this, I need to realize the specific args passed to f, and can't utilize a single function to bind all the capacities I'm keen on (since they have a distinctive argument lists).

1 Answer

0 votes
by (26.4k points)

Try the below code:

>>> from functools import partial

>>> def f(a, b, c):

...   print a, b, c

...

>>> bound_f = partial(f, 1)

>>> bound_f(2, 3)

1 2 3

Are you pretty much interested to learn python in detail? Come and join the python training course to gain more knowledge.

For more details, do check out the below video tutorial...

Browse Categories

...