Python Forum
Interpreter and running a .py file give different outputs - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Interpreter and running a .py file give different outputs (/thread-19777.html)



Interpreter and running a .py file give different outputs - PythonNPC - Jul-14-2019

I have written the following code which gives different output when saved as a .py file and run and on the Python interpreter.

a=100000
b=a
c=100000
print (id(a),id(b),id(c))
Output when saving as .py file and running through command prompt:
2122603390032 2122603390032 2122603390032

All memory addresses are same.


Output via interpreter:
2351093736464 2351093736464 2351093736304

Last memory address is different.


Why is this happening? I am on Python 3.7.4.
What are the differences when running on the interpreter and when running a .py file.


RE: Interpreter and running a .py file give different outputs - Larz60+ - Jul-14-2019

Memory locations are not ever guaranteed to be in the same location. It' assigned at time or operation.


RE: Interpreter and running a .py file give different outputs - PythonNPC - Jul-14-2019

(Jul-14-2019, 10:14 AM)Larz60+ Wrote: Memory locations are not ever guaranteed to be in the same location. It' assigned at time or operation.

Yes, I am aware of that.

But every time I run the .py file, the memory locations are the same, while every time I run the same code via the interpreter the memory locations are different.

So is it that the interpreter runs programs slightly differently as compared to running a .py file directly?


RE: Interpreter and running a .py file give different outputs - PythonNPC - Jul-21-2019

Bump.


RE: Interpreter and running a .py file give different outputs - Gribouillis - Jul-21-2019

Quote:What are the differences when running on the interpreter and when running a .py file.
I already answered a similar question. See here to see if it explains this behaviour.


RE: Interpreter and running a .py file give different outputs - PythonNPC - Jul-21-2019

(Jul-21-2019, 07:25 AM)Gribouillis Wrote:
Quote:What are the differences when running on the interpreter and when running a .py file.
I already answered a similar question. See here to see if it explains this behaviour.

Thank you! Exactly the answer I was looking for.