You may be searching for a generalized mechanism that makes saving and restoring the wanted information somehow more easily. This is called persistence as the term in programming. However, it is not automatic. There are techniques how to implement it. The more complex and specific the implemented system is, the more specific the mechanism can be.
For simple cases, the explicit storing the data into a file is just fine. As Smac89 has shown in https://stackoverflow.com/a/18309156/1346705, the pickle
module is a standard way how to save the status of whole Python objects. However, it is not neccessarily what you always need.
Update: The following code shows the principle. It should be enhanced for the real usage (i.e. should never be used this way, only in tutorial).
#!python3
fname = 'myfile.txt'
def saveInfo(fname, name, money):
with open(fname, 'w', encoding='utf-8') as f:
f.write('{}\n'.format(name))
f.write('{}\n'.format(money)) # automatically converted to string
def loadInfo(fname):
# WARNING: The code expect a fixed structure of the file.
#
# This is just for tutorial. It should never be done this way
# in production code.'''
with open(fname, 'r', encoding='utf-8') as f:
name = f.readline().rstrip() # rstrip() is an easy way to remove \n
money = int(f.readline()) # must be explicitly converted to int
return name, money
name = input('What is your name? ')
money = 5
print(name, money)
# Save it for future.
saveInfo(fname, name, money)
# Let's change it explicitly to see another content.
name = 'Nemo'
money = 100
print(name, money)
# Restore the original values.
name, money = loadInfo(fname)
print(name, money)