Use Python objects to define function signatures.
To programmatically encode relationships between function arguments. Other benefits follow from treating parameters as object properties such as documentation.
Use objects for encoding relationships between arguments. Use a decorator to dynamically create a function.
from obj2sig import paramize, var_property
help(paramize)Takes an object's property attributes to be used as function arguments as follows:
- Function argument order will match the order in which the object properties are defined.
- Property return values are taken as default values. Ellipsis,
..., indicates no default value. - Property return types are used as parameter types.
- Use the special property name 'star' to mark keyword-only arguments.
- Use
var_propertyto mean a variable length argument.
class Params:
@property
def p(self) -> int: return ...
_star_ = ''
@property
def z(self) -> str: return 'z'
@property
def a(self): return 'a'
@var_property
def k(self): return ...
@paramize(Params())
def f():
"""sdfsdf"""
return
ffunctionf
sdfsdf
def f(p: int, *, z: str = 'z', a='a', **k):
class Params1:
@var_property
def l(self): return ...#[]
@paramize(Params1())
def f1():
"""f1"""
return
f1functionf1
f1
def f1(*l):