def some_function(): some_dict = {'random': 'values'} a = some_dict['random'] return a
When is the dictionary some_dict
created in the memory? (first time the function is called?)
When is the dictionary some_dict
destroyed/de-allocated? (when the function returns?)
If so, does that mean the dictionary object will be created every time the function is called?
Should I worry about such things when learning/dealing with python?
Where do I learn about such details of a language? I tried looking in the docs but couldn't find what I was looking for.
It would be appreciated if you could answer all 4 of the above questions!
Although the scope of a static local variable is confined to its function, its lifetime extends to the end of program execution.
2. The lifetime of a variable is the period throughout which the variable exits in the memory of your Python program. The lifetime of variables inside a function is as long as the function executes. These local variables are destroyed as soon as the function returns or terminates.
The lifetime of a variable is the time during which the variable stays in memory and is therefore accessible during program execution. The variables that are local to a method are created the moment the method is activated (exactly as formal parameters) and are destroyed when the activation of the method terminates.
Any variable that you declare inside a function is said to have Local Scope. You can access a local variable can within a function. If you try to access any variable defined inside a function from outside or another function, it throws an error.
some_dict
will be created in memory every single time the function is called.some_function()
.For example, consider the function caller()
which calls the function callee
(some_function()
in your question), as in
def caller(): callee()
From your usage case, we want to call callee()
multiple times, and we need to reuse the same dictionary in callee()
. Let's run through the normal usage cases.
1. Dictionary is generated in callee()
. This is the example in your question.
def caller(): for loop: callee() def callee(): generate dictionary do something with dictionary
In this case, every single time callee()
is called, you have to generate a new dictionary. This is because as soon as callee()
returns, all of its local variables are deallocated. Therefore, you can't "reuse" the same dictionary between different callee()
s.
2. Dictionary is generated in caller()
and passed as an argument to callee()
.
def caller(): generate dictionary for loop: callee(dictionary) def callee(dictionary): do something with dictionary
In this case, you are generating the dictionary once in caller()
, and then passing it to every single callee()
function. Therefore, each time that you call callee()
, you won't need to regenerate the dictionary.
The dictionary is passed by reference, so you aren't passing a huge data structure each time you call callee()
. I'm not going to go in depth about this (you can find a good explanation here), but in essence, there is negligible cost to pass the dictionary as a parameter to callee()
.
When is the dictionary some_dict destroyed/de-allocated? (when the function returns?)
First of all local variables are destroyed as soon as you move away from that scope.
For example in your function when you return you lose all the references to the variables defined like some_dict
. Because you are returning a
and if you do not have something like a_var = some_function()
where a
reference would be caught in a_var
you would lose a
as well.
When is the dictionary some_dict created in the memory? (first time the function is called?)
some_dict = {'random': 'values'} # right here for every method call.
Should I worry about such things when learning/dealing with python?
yes, check the below example
>>> dictionary = {1: 1, 2: 2} >>> def function(): dictionary = {1: 2, 2:1} print dictionary >>> function() {1: 2, 2: 1} >>> dictionary {1: 1, 2: 2}
here you might have assumed that you are re assigning the dictionary but python is creating a local dictionary and is lost soon after you return from function
how to over come the above issue, use global it would tell that you are trying to reference object define outside the function.
>>> def function(): global dictionary dictionary = {1: 2, 2:1} print dictionary >>> function() {1: 2, 2: 1} >>> dictionary {1: 2, 2: 1}
Where do I learn about such details of a language? I tried looking in the docs but couldn't find what I was looking for.
probably the question should be how do I learn such details
, simple with practice and understand the documentation and read over again.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With