lisa.utils.PartialInit#

class lisa.utils.PartialInit(*args, **kwargs)[source]#

Bases: object

Allow partial initialization of instances with curry-like behaviour for the constructor.

Subclasses will be able to be used in this way:

class Sub(PartialInit):
    def __init__(self, x, y):
        self.x = x
        self.y = y

    # This decorator allows the classmethod to be partially applied as
    # well.
    #
    # Note: since PartialInit relies on accurate signatures, **kwargs
    # cannot be used, unless the signature is patched-up with something
    # like lisa.utils.kwargs_forwarded_to()
    @PartialInit.factory
    def factory(cls, x, y):
        return cls(x=x, y=y)


# Bind x=1
# Sub.__init__ not yet called
obj = Sub(1)

# Provide a value for "y", which will trigger a call to the
# user-provided __init__
obj = obj(y=2)

# Make a new instance with value of x=42, and all the other parameters
# being the same as what was provided to build "obj"
obj2 = obj(x=42)

Note

any attribute access on a partially initialized instance will result in a TypeError exception.

Methods

factory()

Decorator to use on alternative constructors, i.e. classmethods that return instances of the class.

Methods#

classmethod PartialInit.factory(f)[source]#

Decorator to use on alternative constructors, i.e. classmethods that return instances of the class.

Once decorated, the classmethod can be partially applied just like the class itself.

Note

@classmethod is applied automatically if not already done by the user.