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.
class Foo:
@staticmethod
def store(factory: Callable[[str], Bar]):
obj = factory("abc") # store or call to get instance
Otherwise, it's expected to be called on an instance, and the first argument should be self
, which receives the instance.
Also, obj
is just a local variable. If you want this to persist, you need to use Foo.obj
.