John Stanford
John Stanford

Reputation: 1093

Lambda in Python class referring to self

I'm trying to define a function in a Python class using lambda and I want to refer to the instance of the class from which it's being called and can't figure out how.

properties.append(pf.moleculeProperties())
properties[-1].name = "Monatomic Hydrogen"
properties[-1].formula = "H"
properties[-1].mass = (1.00795e-3)/(6.022e23)
properties[-1].elecLevels = [[pf.waveNumToJoules(82309.58), 1]]
properties[-1].q = lambda T,V : pf.q_trans(properties[-1],T,V) * pf.q_elec(properties[-1],T,V)

properties.append(pf.moleculeProperties())
properties[-1].name = "Monatomic Oxygen"
properties[-1].formula = "O"
properties[-1].mass = (16.0e-3)/(6.022e23)
properties[-1].elecLevels = [[pf.waveNumToJoules(158.265), 1], [pf.waveNumToJoules(226.977), 1], [pf.waveNumToJoules(15867.862), 1],
           [pf.waveNumToJoules(33792.583), 1], [pf.waveNumToJoules(73768.200), 1], [pf.waveNumToJoules(76794.978), 1], [pf.waveNumToJoules(86625.757), 1]]
properties[-1].q = lambda T,V : pf.q_trans(properties[-1],T,V) * pf.q_elec(properties[-1],T,V)

When I try to call q on the something other than the last member of the list, it seems to evaluate the properties[-1] statement and give me the last member of the list each time. In this example, I'm trying to call the q function on the element corresponding to hydrogen and getting the q function for oxygen.

Upvotes: 0

Views: 1643

Answers (1)

Gary Wisniewski
Gary Wisniewski

Reputation: 1120

You need to evaluate properties in the argument list rather than the body of the lambda so that it binds early. So, define q as:

properties[-1].q = lambda T,V,self=properties[-1] : pf.q_trans(self,T,V) * pf.q_elec(self,T,V)

When you do the above, the assignment to self is evaluated once and becomes bound permanently to the lambda. Otherwise, properties will refer to the parent context (as you've found out).

Upvotes: 2

Related Questions