How would you create a factory based method using lambda?
function signature looks like this. Factory gets a string parameter and returns the instance.
class Foo:
@abstractmethod
def store(factory: Callable[[str], Bar])
obj = factory("abc") # store or call to get instance
class Bar:
def __init__(self):
pass
How to call this method using lambda?
Foo.store(lambda k: Bar(k))
Error
Parameter 'factory' unfilled
Since
store()is called on the class, not an instance, it needs to be declared as a static method.Otherwise, it's expected to be called on an instance, and the first argument should be
self, which receives the instance.Also,
objis just a local variable. If you want this to persist, you need to useFoo.obj.