Suppose I have a class that looks like this:
class Foo:
def __init__(self, method):
self.method = method
def do_action(self):
self.method()
and I want to instantiate it as follows:
some_var = False
def bar():
# Modifies existing variable's value
global some_var
some_var = True
foo = Foo(bar)
how do I do that without having to define the bar() method? I've tried the following and it doesn't work.
foo = Foo(lambda: (some_var := True))
When I do this the IDE tells me there's an identifier expected. Thanks in advance!
EDIT:
Thank you to those who answered, however I didn't really find exactly what I needed. Not sure if it's the best practice, but I ended up using python's exec and it works as intended:
foo = Foo(lambda: exec("some_var = True"))