Python Forum
Running scripts and location of saved interpreted user-defined classes and functions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Running scripts and location of saved interpreted user-defined classes and functions
#4
(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.)
Reply


Messages In This Thread
RE: Running scripts and location of saved interpreted user-defined classes and functions - by micseydel - Aug-25-2020, 03:43 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  "Name is not defined" when running a class lil_e 6 4,490 Jan-12-2023, 11:57 PM
Last Post: lil_e
  Using .pb saved model for object detection hobbyist 2 1,250 Aug-03-2022, 05:55 AM
Last Post: hobbyist
  Are list/dict comprehensions interpreted really sequentially? anata2047 3 1,543 May-31-2022, 08:43 PM
Last Post: Gribouillis
  User-defined function to reset variables? Mark17 3 1,780 May-25-2022, 07:22 PM
Last Post: Gribouillis
  Multiple user defined plots with secondary axes using for loop maltp 1 1,603 Apr-30-2022, 10:19 AM
Last Post: maltp
  bytes object saved as .mp4 jttolleson 10 6,124 Feb-25-2022, 02:42 PM
Last Post: jttolleson
  Definitions in User-Created Functions and For Loops new_coder_231013 6 2,278 Dec-29-2021, 05:51 AM
Last Post: ndc85430
  Why built in functions are defined as class? quazirfan 5 2,976 Oct-23-2021, 01:20 PM
Last Post: Gribouillis
  Running python scripts from github etc pacmyc 7 3,862 Mar-03-2021, 10:26 PM
Last Post: pacmyc
  [SOLVED] Requiring help running an old Python script (non Python savvy user) Miletkir 13 5,727 Jan-16-2021, 10:20 PM
Last Post: Miletkir

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020