Aug-25-2020, 03:43 AM
(Aug-25-2020, 03:09 AM)bowlofred Wrote: One possibility is that you pickle/dill the function to bytecode on disk.I was skeptical that you can pickle a function, so I did some testing:
>>> def func(): ... print("Hello, world!") ... return "hiss" ... >>> func() Hello, world! 'hiss' >>> from pickle import dumps, loads >>> cucumber = dumps(func) >>> loads(cucumber) <function func at 0x10ee8cee0> >>> # but wait... >>> del func >>> func = loads(cucumber) Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: Can't get attribute 'func' on <module '__main__' (built-in)>(You can write the pickle to disk, instead of saving it as a byte string in memory, but it will have the same problem.)
You really should stick to source code. If you did keep compiled code (like in a .pyc file), it would be incompatible with future Python executables. Python source code is portable. You can look at it and know what it does. (I'm terrified at the idea of someone storing compiled binaries without a reference to the source.)