0

I want a variable in my program which gets initialized only once that is when the program is run for the first time and the next time I run the program, the value the variable is holding must have the last modification done by the same program when previously run.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • environment variables, files or database can be used – Priyesh Kumar May 30 '17 at 19:42
  • 4
    You need to save the variable in a file and load it every time you execute your program. you can use pickle.dump() and pickle.load() or a plain text file, depends from the variable. – alec_djinn May 30 '17 at 19:42
  • @PriyeshKumar You cannot change an environment variable from a program. – DYZ May 30 '17 at 19:43
  • @DYZ - of course you can. – zwer May 30 '17 at 19:44
  • @zwer Are you sure? – DYZ May 30 '17 at 19:45
  • 1
    @DYZ. You most certainly can. It won't offer the level of persistence OP is looking for though. – Mad Physicist May 30 '17 at 19:45
  • you could do something fancy like changing the last line of the script from within the script to hold that value, but if you dont want to indulge in such madness then you will need to have a file of some sort like @alec_djinn suggested – Nullman May 30 '17 at 19:46
  • Unfortunately, SO is not a free coding site. Dumping a list of requirements and expecting a dump of code in return will most likely leave you quite disappointed and get your question closed down. – Mad Physicist May 30 '17 at 19:46
  • @MadPhysicist You can modify your _copy_ of a variable, but not the original value known to the shell. – DYZ May 30 '17 at 19:47
  • @Nullman. Technically that's a subset of dumping to a file, just like pickling is. – Mad Physicist May 30 '17 at 19:47
  • @MadPhysicist yes, but it avoids having to manage an extra file, which i a gigantic hassle sometimes – Nullman May 30 '17 at 19:48
  • 3
    @DYZ. Correct. You can only affect children of your process, really. Dumping to disk is a much more effective. – Mad Physicist May 30 '17 at 19:48
  • @Nullman. I suspect managing a tiny file with well specced out content will be much easier to manage than a self-modifying script. – Mad Physicist May 30 '17 at 19:49
  • @MadPhysicist but not as educational! :) you are right in the vast majority of use cases, i was just offering an interesting, if maybe not the most practical, alternative – Nullman May 30 '17 at 19:50
  • @DYZ - `subprocess.call(["setx", "/M", "VARIABLE", "VALUE"])` on Windows, `with open("~/.bashrc", "a") as f: f.write("export VARIABLE=VALUE\n")` on Linux, followed by a local `set / export` (respectively) call to alter the running environment, followed by modification of the `os.environ` list in Python to top it all up. The only environment that won't be affected is the shell that started the Python process and only until it lives - the next shell session will load the variable automatically. Of course, I'm not saying that's a good solution to OPs problem, far from it, but you can do it. – zwer May 30 '17 at 19:53
  • @zwer This does not change the value of a shell variable, it changes its definition. How is this different from writing into a file? – DYZ May 30 '17 at 19:56
  • @DYZ - it's not. I never said it's a good idea or a better alternative, I just said that you can do it. – zwer May 30 '17 at 19:58
  • @Nullman. Sure, doing stuff like that is fun. The most educational thing in this case, though, is to [close the question](https://meta.stackoverflow.com/q/256328/2988730). – Mad Physicist May 30 '17 at 20:04
  • You could save the variable in a file with the [`pickle`](https://docs.python.org/3/library/pickle.html#module-pickle) module (see [this answer](https://stackoverflow.com/questions/4529815/saving-an-object-data-persistence/4529901#4529901)) and check if the file exists or not at the beginning of script to determine how to set its value. – martineau May 30 '17 at 20:24

0 Answers0