Python Forum
Questions on variable changing between files. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Questions on variable changing between files. (/thread-3002.html)

Pages: 1 2


Questions on variable changing between files. - Sello - Apr-24-2017

I am starting out in Python. I have a project I am doing and is the reason I took this language. I'm looking to build my own version of card game and the scope of the project involves a lot of variable changing. For now I'm working on a "Profile" file that will contain wins, losses, etc, and a username, you can expect the code to be very basic. Anyhow those values will need to be changed accordingly.

One thing I do not understand clearly is how to change variables between files. I've learned how to read, write, open, close and delete the contents of files. But I can't seem to change variables within them. What I mean by that is this

File A(just short example)

# This is File A
from fileb import bvar

bvar = 10
It will print 10, but bvar on File B is not changed to 10. It is still 0.

I have read up on globals, and other methods but none seem to physically change the variable on another file. How can I do this?


RE: Questions on variable changing between files. - wavic - Apr-24-2017

Hm! It won't change as the file is already written to the disk.
You can load it, do what you want with its content and then you can write it back.

You are using the import statement to get the variable. Is this file a python one? With .py at the end of the name.
Anyway, to permanently store some data you can try do save it as json or csv for example. Python has built-in tools to handle both.


RE: Questions on variable changing between files. - Sello - Apr-24-2017

Yeah, its a python file. And I'll look into those options.


RE: Questions on variable changing between files. - Kebap - Apr-24-2017

(Apr-24-2017, 07:04 AM)Sello Wrote: I'm looking to build my own version of card game and the scope of the project involves a lot of variable changing. For now I'm working on a "Profile" file that will contain wins, losses, etc, and a username, you can expect the code to be very basic. Anyhow those values will need to be changed accordingly.

OK so you want to save values. This is usually done with some kind of database or maybe even simple text file.

You do not need to save it to another python script and/or import like this. I think you may be confusing things here.


RE: Questions on variable changing between files. - wavic - Apr-24-2017

Since you want to store Python objects consider pickle module too. Pay attention of the warning in the link. The red paragraph


RE: Questions on variable changing between files. - volcano63 - Apr-24-2017

(Apr-24-2017, 08:25 AM)wavic Wrote: Since you want to store Python objects consider pickle module too. Pay attention of the warning in the link. The red paragraph
Unless complex object state is to be saved, pickle is too heavy  - json is simpler.


RE: Questions on variable changing between files. - wavic - Apr-24-2017

JSON stores all in plain text. For sure, pickle has its limitations but is not heavy at all.
Here is what can be pickled: https://docs.python.org/3.5/library/pickle.html#what-can-be-pickled-and-unpickled


RE: Questions on variable changing between files. - Kebap - Apr-24-2017

Sorry, forgot to include the link to our python-forum tutorial: Database - the easy way (dataset)


RE: Questions on variable changing between files. - snippsat - Apr-24-2017

# This is File A
from fileb import bvar

bvar = 10
Think of what you do here,you just overwrite the import bvar
So the import has no meaning here.
Eg:
# fileb.py
bvar = 10
# filea.py
from fileb import bvar

avar = 99
print('var for filea {}\nvar from fileb {}'.format(avar, bvar))
Output:
var for filea 99 var from fileb 10
So here i don't overwrite the import and can use value from fileb in filea.


RE: Questions on variable changing between files. - Sello - Apr-24-2017

Thanks for the help everyone. :D