is variable lost once a module returns to main script?

16 views
Skip to first unread message

s...@weacceptyou.com

unread,
Oct 11, 2016, 4:24:53 AM10/11/16
to Python Programming for Autodesk Maya
hello,

i am calling a module which creates a variable, i dont need to return this variable to the main script once the module is done its job. But does this mean the next time the module is called that variable created before is removed from memory?

thanks,
Sam

Marcus Ottosson

unread,
Oct 11, 2016, 4:38:35 AM10/11/16
to python_in...@googlegroups.com

Would it be possible to show an example?

E.g.

my_module.py

my_variable = "Hello"

main_script.py

import my_module
my_module.my_variable = "World"


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/7b37dd20-7c21-4ed7-9416-dc204537b415%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Justin Israel

unread,
Oct 11, 2016, 4:54:31 AM10/11/16
to Python Programming for Autodesk Maya


On Tue, 11 Oct 2016, 9:24 PM <s...@weacceptyou.com> wrote:
hello,

i am calling a module which creates a variable, i dont need to return this variable to the main script once the module is done its job. But does this mean the next time the module is called that variable created before is removed from memory?

It seems that some of your terms are mixed up so it's hard to know what you might be referring to. Modules aren't callable, and are a container usually representing objects parsed from a file loaded into the interpreter (but there are other sources). 

Do you actually mean global variables in a module? Or do you mean local variables created within a function that lives within a module? 

Variables created in the global scope of the module will live for as long as the module stays loaded in the interpreter. This is really just a fancy term for it living in sys.modules, and also once no one keeps a reference to it. 

Variables created in the scope of a function body are local to that function and are destroyed when the stack frame of that function call is cleaned up, at the end of the function. 



thanks,
Sam

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

s...@weacceptyou.com

unread,
Oct 11, 2016, 10:51:33 AM10/11/16
to Python Programming for Autodesk Maya
yo sorry,

i mean theres a function in a module that i call. i meant after the function in a module creates a variable, is the next time that function is called the variable created previously is no longer existing?.

but i guess you already answered it. Though im not sure what the stack frame thing is.

thanks guys,
Sam

Justin Israel

unread,
Oct 11, 2016, 2:41:41 PM10/11/16
to Python Programming for Autodesk Maya
Cool, thank you for clarifying. 
Yes the variable will be gone when the call to the function ends. 

When you call a function, some memory is allocated, so hold the variables that you newly create within that function. This is called "stack space". If you have ever heard of a stack overflow, it is when you consume more memory than was allocated for your functions stack space, and you crash. 
In python, a thing called a stack frame is created and put into a list with all of the other functions that are callers before you. That is,  if a() calls b() and b()  calls c(), your stack would look like :

[..., a_frame, b_frame, c_frame] 

This can actually be inspected from the "inspect" module, and is also how tracebacks (crashes) know that history of where you are in code. 

Ok so all those details aside, in summary, stack is where those local function variables live, and the stack space for the function dies when the function scope is over. Of you want any of that data to live on, then it either needs to be returned or stored on an object that will continue to live (class instance, global module scope, ...) 



thanks guys,

Sam

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

s...@weacceptyou.com

unread,
Oct 11, 2016, 4:33:57 PM10/11/16
to Python Programming for Autodesk Maya
perfect thanks Justin, i get it now, cheers,

Sam
Reply all
Reply to author
Forward
0 new messages