1

I am possibly very naive, but I find the following behaviour unexpected.

Introduction: I need a wrapper to address dynamically the methods of my own class, model. I am trying to use a dict to have a separate entry for each of a given number of members of the class that are dynamically requested. I link the dict keys to the chosen members iteratively and I find that the doc string is preserved, but the methods get overwritten by the last item in the iteration, despite their distinct keys. Here is a snippet where I reproduce the behaviour with numpy, in place of my own class.

import numpy as np
name = ["sin","cos"]

bnd = {}
print('Within the defining loop, it works!\n')
for nam in name:
    # useless indirect function (with doc string)
    # equivalent to sin(2*pi*x) 
    # or to cos(2*pi*x)
    bnd[nam] = lambda x, par: np.__getattribute__(nam)(x*par)
    bnd[nam].__doc__ = '"""'+nam+'"""'
    print('bnd doc in-loop: {} = {}'.format(nam,bnd[nam].__doc__))
    print('bnd method in-loop {}(0,2*pi) = {}'.format(nam,bnd[nam](0,2*np.pi)))

print('\n    However after the loop...')
print('bnd keys {}'.format(bnd.keys()))
print('\nfirst function doc: {}'.format(bnd["sin"].__doc__))
print('doc is preserved, but instead the method')
print('(should be sin(2 pi *0)) yields {}'.format(bnd["sin"](0,2*np.pi)))
print('\nsecond trial_function doc: {}'.format(bnd["cos"].__doc__))
print('doc is preserved, again, and this time the method')
print('(should be cos(2 pi *0)) yields  correctly {}'.format(bnd["cos"](0,2*np.pi)))
print('\nSummary: bnd[nam] gets overwritten by the last lambda definition in the loop. \n\nWhy????') 

If you run the code you get the following

Within the defining loop, it works!

bnd doc in-loop: sin = """sin"""
bnd method in-loop sin(0,2*pi) = 0.0
bnd doc in-loop: cos = """cos"""
bnd method in-loop cos(0,2*pi) = 1.0

    However after the loop...
bnd keys dict_keys(['sin', 'cos'])

first function doc: """sin"""
doc is preserved, but instead the method
(should be sin(2 pi *0)) yields 1.0

second trial_function doc: """cos"""
doc is preserved, again, and this time the method
(should be cos(2 pi *0)) yields  correctly 1.0

Summary: bnd[nam] gets overwritten by the last lambda definition in the loop. 

Why????

which I hope is clarifying my question.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • Why wouldn't you tag this with [python]! – juanpa.arrivillaga Jan 10 '22 at 02:20
  • I was pointed to https://stackoverflow.com/questions/10452770/python-lambdas-binding-to-local-values by https://github.com/bonfus, which already contains an answer: in brief, I was overlooking that nam also is a lambda parameter, and, as such, it takes value at execution time, not at definition time. The link shows that ```bnd[nam] = lambda x, par, nam=nam: np.__getattribute__(nam)(x*par)``` does the trick. Added in the code as a comment now – Roberto De Renzi Jan 10 '22 at 09:09
  • Please don't answer your question inside the question itself. If you want, you are free to post an answer to your own question, but anyway this is a duplicate so please mark it as such – Tomerikoo Jan 10 '22 at 09:26
  • This question was marked as a duplicate, but I must say it's one of the sneakiest problems I've seen in Python. I wouldn't know how to search for it. – Mark Ransom Jan 10 '22 at 18:45

1 Answers1

1

As others have explained. There is only one variable nam in your function. When you create the lambda, it doesn't capture the value of nam as it was when created the lambda. Rather all the lambdas share the same nam, and it has whatever value it had at the end of the function.

There are several get arounds. Essentially you need to capture nam as a variable.

The "official" way to do this is that you could just write:

   def attribute_getter(nam):
      return lambda x, par: np.__getattribute__(nam)(x*par)
   bind[nam] = attribute_getter(nam)

The quick and dirty solution is:

   bind[nam] = lambda x, par, nam=nam: np.__getattribute__(name)(x * par)

The variable nam is turned into an optional which is set to the current value of nam.


Edit: Changed "program" to "function".

Frank Yellin
  • 9,127
  • 1
  • 12
  • 22