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
#1
My general inception If the scripts containing user-defined functions and user-defined classes. And the Python shell is closed. These functions and classes are no longer available to be called or to have their methods or attributes accessed anymore.

My queries are the following:
1) Where are these user-defined classes and functions saved when running a script? for example if I run a script containing the function

def hello():
    print("Hello, World!")
and then I type in hello
I get the following:

<function hello at 0x0000015ED9263CA0>

In other words, what is this location 0x0000015ED9263CA0? And where can I find it? And if I run the script containing the function again, I get a different location at 0x00000119A5DE3CA0. What is exactly happening?

2) How can these user-defined classes and functions be saved permanently- to be called or accessed even if a shell is restarted?

3) When is the Python session data and interpreted script become unavailable anymore?
Reply
#2
1) So in CPython, my understanding is that that number is the memory address of the function. That said, Jython and other implementations may not follow that. I wouldn't worry about it though.
2) I'd recommend saving the source code to a .py file.
3) That's an odd question... I'd basically say that it's when the process goes out of memory, though the technical person in me realizes there might be other answers there.

If you want to safe user defined functions, just save the Python source code to a file. Maybe your questions are a result of using some other language, but you shouldn't be worrying about memory addresses in Python, generally speaking.
Reply
#3
It would help to know what you are trying to do with that information.

One possibility is that you pickle/dill the function to bytecode on disk. Then the next time you run the program you can load it in and call it. It's not the exact same function (since the old one will be destroyed when the python process exits), but it doesn't have to be recompiled.

In general, the overhead you're avoiding by this isn't worth it, and redefining a new function from source is preferred. But if you could give some more information, it might help.
Reply
#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


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