Function¶
- class audobject.resolver.Function[source]¶
Function resolver.
Encodes source code of function and dynamically evaluates it when the value is decoded again.
Note that a decoded function might raise a
NameError
, if it relies on objects or functions that are not defined or imported inside the function. For instance, the following example will raise an error sinceplus_1()
relies on_plus_1()
, which is defined outside the function:def _plus_1(x): return x + 1 def plus_1(x): return _plus_1(x) # calls local function -> not serializable resolver = Function() encoded_value = resolver.encode(plus_1) del _plus_1 decoded_value = resolver.decode(encoded_value) decoded_value(1)
Examples
>>> resolver = Function() >>> def func(x): ... return x * 2 >>> func(5) 10 >>> encoded_value = resolver.encode(func) >>> encoded_value 'def func(x):\n return x * 2\n' >>> decoded_value = resolver.decode(encoded_value) >>> decoded_value(5) 10