Python Forum

Full Version: Running scripts and location of saved interpreted user-defined classes and functions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
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.
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.
(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.)