How to manipulate function inside lambda using python?

I would like to manipulate a list of lambda functions. This list of lambda functions contains the same function inside it that I would like to manipulate so that the function inside it has an additional argument

See my example below.

def base_function(*args):
    for arg in args:
        print arg

list_of_functions = [
    lambda new_arg: base_function(new_arg,'arg_1'),
    lambda new_arg: base_function(new_arg,'arg_2'),
    lambda new_arg: base_function(new_arg,'arg_3')
]

Is it possible to change this list of functions as follows without rewriting the entire list of functions

list_of_functions = [
    lambda new_arg: base_function(new_arg,'arg_1','another_arg'),
    lambda new_arg: base_function(new_arg,'arg_2','another_arg'),
    lambda new_arg: base_function(new_arg,'arg_3','another_arg')
]

See below for another example of what I'm trying to do. I have the following:

def simple_function(arg,capitalize=False):
    if capitalize:
        print arg.upper()
    else:
        print arg 


list_of_functions = [lambda key_word: simple_function(key_word),lambda key_word: simple_function(key_word)]

But I would like to change list_of_functions to the following without overwriting either list_of_functions above or simple_function:

desired_list_of_functions = [lambda key_word: simple_function(key_word,capitalize=True),lambda key_word: simple_function(key_word,capitalize=True)]
+6
source share
3 answers

, , , @austin-hastings:

# first example from the OP

def base_function(*args):
    for arg in args:
        print arg

list_of_functions = [
    lambda new_arg: base_function(new_arg,'arg_1'),
    lambda new_arg: base_function(new_arg,'arg_2'),
    lambda new_arg: base_function(new_arg,'arg_3')
]

# solution:

from functools import partial

new_list_of_functions = [
  partial(fn, 'another_arg') for fn in list_of_functions
]

, @austin-hastings .


, list_of_functions .

:

desired_list_of_functions = [
  lambda key_word: simple_function(key_word,capitalize=True),
  lambda key_word: simple_function(key_word,capitalize=True)
]

:

desired_list_of_functions = [
    partial(fn, capitalize=True) for fn in list_of_functions
]

partial (fn, *args, **kwargs), .


, , :

def simple_function(arg,capitalize=False):
    if capitalize:
        print (arg.upper())
    else:
        print (arg)


list_of_functions = [lambda key_word: simple_function(key_word),lambda key_word: simple_function(key_word)]

# will print 'foo' twice
for fn in list_of_functions:
    fn('foo')

simple_function = partial(simple_function, capitalize=True)

# will print 'FOO' twice
for fn in list_of_functions:
    fn('foo')

, lambda list_of_functions simple_function, - .

, simple_function .

0

:

def simple_function(arg, capitalize=False):
    if capitalize:
        print arg.upper()
    else:
        print arg

#this function takes as its argument a lambda function which
#is assumed to merely call another global function defined within
#the same module  
def transf(l):
    #find out which function is being called from within the lambda
    f = globals()[l.__code__.co_names[0]]

    #return a lambda calling the same function with customized keyword argument
    return lambda arg: f(arg, capitalize=True)

list_of_functions = [lambda key_word: simple_function(key_word),lambda key_word: simple_function(key_word)]

new_list_of_functions = list(map(transf, list_of_functions))

for f in list_of_functions:
    f('x')

for f in new_list_of_functions:
    f('x')
0

, partial, . , , ( , ):

def base_function(*args):
    for arg in args:
        print arg

list_of_functions = [
    lambda new_arg: base_function(new_arg,'arg_1'),
    lambda new_arg: base_function(new_arg,'arg_2'),
    lambda new_arg: base_function(new_arg,'arg_3')
]

for f in list_of_functions:
    f('x')

import functools

class partial_atend(functools.partial):
    def __call__(self, *args, **kwargs):
        f=self.func
        args += self.args
        kw = self.keywords.copy()
        kw.update(kwargs)
        return f(*args, **kw)

list_of_functions = [
    partial_atend(base_function, 'arg_1'),
    partial_atend(base_function, 'arg_2'),
    partial_atend(base_function, 'arg_3'),
]

# Modify args:
list_of_functions = [ partial_atend(f.func, *(f.args+('another_arg',)), **f.keywords) for f in list_of_functions]

# Make the call:
print "Make the call(s)..."
for f in list_of_functions:
    f('x')

, : , , , partial_atends, ... . , . ( ), , , .

EDIT:

capitalize=True, keywords:

Change first base_function kwargs:

def base_function(*args, **kwargs):
    for arg in args:
        print arg
    for k,v in kwargs.items():
        print("{}={}".format(k,v))

Then add this code:

# Modify kwargs, add `capitalize=True`:
list_of_functions = [ partial_atend(f.func, *f.args, capitalize=True, **f.keywords) for f in list_of_functions]

And print the new lambdas:

print "Make the call(s)...capitalized"
for f in list_of_functions:
    f('x')
0
source

Source: https://habr.com/ru/post/1016774/


All Articles