Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Saving variables in memory
#1
Hi,
I understand Python saves variables in memory, right? As it does so, does it create any logs or files with a reference to the content of the variables? I am a beginner and I don't understand very well what happens behind the scenes. Thanks in advance!
Reply
#2
What is you question?

Are you wondering how Python makes a connection between a variable name and a value?
Are you asking what kind of structures Python has for saving variable values?
Are you asking how logging works in Python?
Are you asking how to save values so they can be loaded when you start your program?
Reply
#3
(Mar-08-2021, 04:36 PM)deanhystad Wrote: What is you question?

Are you wondering how Python makes a connection between a variable name and a value?
Are you asking what kind of structures Python has for saving variable values?
Are you asking how logging works in Python?
Are you asking how to save values so they can be loaded when you start your program?


Thanks for the reply. It helps to know that my question was not as specific as it should have been. Splitting it into all those more specific questions already helps. I'll do some reading into each one.
Reply
#4
(Mar-08-2021, 03:48 PM)pprod Wrote: I understand Python saves variables in memory, right? As it does so, does it create any logs or files with a reference to the content of the variables?
>>> n = 99
>>> id(n)
1966484706736

>>> help(id)
Help on built-in function id in module builtins:

id(obj, /)
    Return the identity of an object.
    
    This is guaranteed to be unique among simultaneously existing objects.
    (CPython uses the object's memory address.)

>>> globals()['n']
99
Variable is stored somewhere in the free memory that's available.
Also stored in global namespace,which is a internal dictionary(globales()) that python use.
>>> a = 'hello world'
>>> b = 'hello world'
>>> a is b
False
>>> a == b
True
is will return True if two variables point to the same object in memory.
== if the objects referred by the variables are equal.
>>> a = 'hello world'
>>> b = a
>>> a is b
True
>>> a == b
True
Other languages have variables Python has names.
So over here is a and b name tagged to same memory address.
pprod likes this post
Reply
#5
(Mar-08-2021, 06:37 PM)snippsat Wrote:
(Mar-08-2021, 03:48 PM)pprod Wrote: I understand Python saves variables in memory, right? As it does so, does it create any logs or files with a reference to the content of the variables?
>>> n = 99
>>> id(n)
1966484706736

>>> help(id)
Help on built-in function id in module builtins:

id(obj, /)
    Return the identity of an object.
    
    This is guaranteed to be unique among simultaneously existing objects.
    (CPython uses the object's memory address.)

>>> globals()['n']
99
Variable is stored somewhere in the free memory that's available.
Also stored in global namespace,which is a internal dictionary(globales()) that python use.
>>> a = 'hello world'
>>> b = 'hello world'
>>> a is b
False
>>> a == b
True
is will return True if two variables point to the same object in memory.
== if the objects referred by the variables are equal.
>>> a = 'hello world'
>>> b = a
>>> a is b
True
>>> a == b
True
Other languages have variables Python has names.
So over here is a and b name tagged to same memory address.

Thanks for the insight, snippsat!
Reply
#6
Stop using the reply button
Reply
#7
Python, like virtually all computer languages, stores variables in memory.

Something to look into is that Python will use SQLite to store database values in memory.
Reply
#8
(Mar-08-2021, 11:16 PM)jefsummers Wrote: Something to look into is that Python will use SQLite to store database values in memory.
@jefsummers, this is unclear and confusing. Would you elaborate what you mean? Do you refer to general in-memory option for sqlite3 database (which I think has nothing to do with OP question). Or do you mean that internally python uses sqlite database for names/variables management?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
Did not mean to be confusing, rather to make OP aware of some of the capabilities of Python. The ability to use a database to store information in memory is, to me, a nice option. Just hoped to pique his/her curiosity as clearly the OP was posting a question aimed at broadening knowledge, rather than answering a particular issue at hand.
buran likes this post
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020