Before I start, let me say I know what I am trying to do is unorthodox and perhaps people will have an aversion to the whole concept, however I am curious as to whether it is possible.
So here is the question.
Given an instance k of a class Kthat has an integer attribute z initialised to zero; can we call a lambda function from within k that modifies k.z?
An example would be simply incrementing k.z from 0 to 1.
I've played around a little to see if I could get it to work, and despite success I have included some of that code below.
class K():
def __init__(self):
self.z = 0
self.functions = [
lambda : "Returns string",
lambda : self.z, # returns value of z
# lambda : self.z+=1, # Fails to execute
]
k = K()
print(k.functions[0]())
print(k.functions[1]())
print(k.z)