pg.Functor¶
Accessible via pg.Functor
, pg.symbolic.Functor
.
- class Functor[source]¶
Bases:
pg.Object
,pg.object_utils.Functor
Symbolic functions (Functors).
A symbolic function is a symbolic class with a
__call__
method, whose arguments can be bound partially, incrementally bound by attribute assignment, or provided at call time.Another useful trait is that a symbolic function is serializable, when its definition is imported by the target program and its arguments are also serializable. Therefore, it is very handy to move a symbolic function around in distributed scenarios.
Symbolic functions can be created from regular function via
pg.functor
:# Create a functor class using @pg.functor decorator. @pg.functor([ ('a', pg.typing.Int(), 'Argument a'), # No field specification for 'b', which will be treated as any type. ]) def sum(a, b=1, *args, **kwargs): return a + b + sum(args + kwargs.values()) sum(1)() # returns 2: prebind a=1, invoke with b=1 (default) sum(a=1)() # returns 2: same as above. sum()(1) # returns 2: bind a=1 at call time, b=1(default) sum(b=2)(1) # returns 3: prebind b=2, invoke with a=1. sum(b=2)() # wrong: `a` is not provided. sum(1)(2) # wrong: 'a' is provided multiple times. sum(1)(2, override_args=True) # ok: override `a` value with 2. sum()(1, 2, 3, 4) # returns 10: a=1, b=2, *args=[3, 4] sum(c=4)(1, 2, 3) # returns 10: a=1, b=2, *args=[3], **kwargs={'c': 4}
Or created by subclassing
pg.Functor
:class Sum(pg.Functor): a: int b: int = 1 def _call(self) -> int: return self.a + self.b
Usage on subclassed functors is the same as functors created from functions.
Attributes:
Returns bound argument names.
Returns the names of bound argument whose values are the default.
Returns if all arguments of functor is bound.
Returns the names of bound arguments whose values are not the default.
Returns user specified argument names.
Returns unbound argument names.
- property default_args: Set[str][source]¶
Returns the names of bound argument whose values are the default.