简体   繁体   中英

Change variable permanently

I'm writing a small program that helps you keep track of what page you're on in your books. I'm not a fan of bookmarks, so I thought "What if I could create a program that would take user input, and then display the number or string of text that they wrote, which in this case would be the page number they're on, and allow them to change it whenever they need to?" It would only take a few lines of code, but the problem is, how can I get it to display the same number the next time I open the program? The variables would reset, would they not? Is there a way to permanently change a variable in this way?

You can store the values in a file, and then load them at start up.

The code would look somewhat like this

variable1 = "fi" #start the variable, they can come from the main program instead
variable2 = 2

datatowrite = str(variable1) + "\n" + str(variable2) #converts all the variables to string and packs them together broken apart by a new line

f = file("/file.txt",'w')
f.write(datatowrite) #Writes the packed variable to the file
f.close() #Closes the file !IMPORTANT TO DO!

The Code to read the data would be:

import string

f = file("/file.txt",'r') #Opens the file
data = f.read() #reads the file into data
if not len(data) > 4: #Checks if anything is in the file, if not creates the variables (doesn't have to be four)

    variable1 = "fi"
    variable2 = 2
else:
    data = string.split(data,"\n") #Splits up the data in the file with the new line and removes the new line
    variable1 = data[0] #the first part of the split
    variable2 = int(data[1]) #Converts second part of strip to the type needed

Bear in mind with this method the variable file is stored in plaintext with the application. Any user could edit the variables and change the programs behaviour

You need to store it on disk. Unless you want to be really fancy, you can just use something like CSV, JSON, or YAML to make structured data easier.

Also check out the python pickle module.

Variables have several lifetimes:

  • If they're inside of a block of code, their value only exists for that block of code. This covers functions, loops, and conditionals.
  • If they're inside of an object, their value only exists for the life of that object.
  • If the object is dereferenced, or you leave your code of block early, the value of the variable is lost.

If you want to maintain the value of something in particular, you have to persist it. Persistence allows you to write a variable out to disk (and yes, databases are technically out-to-disk), and retrieve it at a later time - after the lifetime of the variable has expired, or when the program is restarted.

You've got several options for how you want to persist the location of their page - a heavy-handed approach would be to use SQLite ; a slightly less heavy handed approach would be to unpickle the object, or simply write to a text file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM