0

I know I can store variables in other files using pickle and open but I was wondering if I could do it like this
player.py:

class Ore():
    class Metal():
        Coal = 0
        Lead = 0
        Zinc = 0
        Iron = 0
        Tin = 0
        Gold = 0
        Gem = 0
    class Gas():
        Hydrogen = 0
        Oxygen = 0
        Nitrogen = 0
        Neon = 0
        Xenon = 0

and then in another file run

import player
Ore.Metal.Coal = 1
print(Ore.Metal.Coal)

The program returns 1 but where is this '1' Stored? and why if I run the same file after without the "Ore.Metal.Coal = 1" line does it return 0? Is there a way I can make it return 1 by storing 1 in the file?

Thanks for reading

  • Yes, you can modify the file itself with a text editor. – ForceBru Sep 02 '18 at 16:45
  • The point was to have a program edit the file –  Sep 02 '18 at 16:46
  • Did you consider JSON to save and load data? https://docs.python.org/3/library/json.html – Eric Duminil Sep 02 '18 at 17:03
  • @9291Sam, you also can have your program parse the file and insert data there. It's impossible to edit the file like you're trying here because Python files are _text_, not objects or anything like that. – ForceBru Sep 02 '18 at 17:20

3 Answers3

1

Please note, the following explanation is a little simplified:

Your variables are stored in the RAM of your computer as long as your program is running. As soon as you restart it, the changes will be lost!

Let's see why: Your program is basically a text file that is taken by the python interpreter and read. For every variable python will allocate a small chunk of RAM memory. As long as your program is running, python can read or write that chunk of memory. That happens if you access the variable.

As soon as python is "done" with your code it will free all the RAM it allocated. That way, other processes can use the RAM. Your variables will be lost.

You shouldn't change that process. The fact that your variables are lost is intended. But there are still ways to store your data.

There are several ways to do this. Read the Python documentation on how to write into a file. You can build a class that writes down all these values when your program terminates and reads them when you start it up again. A simple text file might do or you could search for packages that will do that for you. Pickle might be a start, but there might be better ways to do this.

Alternatively you can build a function that saves your values whenever they change. This can generate a lot of write/read accesses to your hard drive so be careful when you do this.

If you want to make sure that your game is safe from cheating you can think about encoding your stats so you can't edit them with a text editor.

Also, I recommend that you should read more about classes. Currently you use the class Gas within the class Ore. Does that makes sense? Is a Gas an Ore?

Also, do you want to write down every item in the game? How about a dictionary to count your ores?

Also check out pygames. It is a little dated but they have tutorials that might help you.

N Chauhan
  • 3,407
  • 2
  • 7
  • 21
0

Firstly, the variable will not store in the file because the variable only exists during explaining. 'Ore.Metal.Coal' is a static variable of the class 'Ore.Metal'. Static variable(without self) is a shared variable of the class and class entities. You will get 'Ore.Metal.Coal=1' only before you change 'Ore.Metal.Coal = 1' and the interpreter stoping explaining the codes.

So, there is no choice to storing 1 in the file, unless the code in another file edit the 'player.py' make 'Ore.Metal.Coal=1'

Wjh
  • 1
0

When a python module "foo" is imported, the plain-text "foo.py" file is interpreted into Python byte-code and executed. Any variables in the file-scope of "foo.py" will be accessible as members of a python object named "foo". When another file also asks to import "foo", python will just provide it with the existing "foo" python object. Anything in the foo module can be modified by any other module, assuming the interpreter pointed to the existing "foo" instance stored in memory.

Unless "foo" is imported with a different signature, or "foo" is explicitly "reloaded", Python will only read "foo.py" once. The foo instance is all your code modified - Python does not modify its source files. If you want to store changes in something other than memory, you need to explicitly serialize and de-serialize it. Pickle is one example of such an interface. You can read more about this in the standard library.