Python Forum
Questions on variable changing between files.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Questions on variable changing between files.
#1
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?
Reply
#2
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Yeah, its a python file. And I'll look into those options.
Reply
#4
(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.
Reply
#5
Since you want to store Python objects consider pickle module too. Pay attention of the warning in the link. The red paragraph
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
(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.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#7
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/pick...-unpickled
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
Sorry, forgot to include the link to our python-forum tutorial: Database - the easy way (dataset)
Reply
#9
# 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.
Reply
#10
Thanks for the help everyone. :D
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Keeping a value the same despite changing the variable it was equated to TheTypicalDoge 2 1,454 Mar-13-2022, 10:50 PM
Last Post: Yoriz
  changing Python files to .exe alok 2 2,231 Jul-20-2021, 02:49 PM
Last Post: alok
  Changing a variable's name on each iteration of a loop rix 6 84,635 Jan-03-2020, 07:06 AM
Last Post: perfringo
  how write variable in files and retreive it frank 2 2,054 Jan-30-2019, 12:53 AM
Last Post: frank
  int(variable) not changing variable to an integer StoopidChicken 2 2,938 Jan-11-2018, 10:30 AM
Last Post: StoopidChicken
  Discord bot that asks questions and based on response answers or asks more questions absinthium 1 38,777 Nov-25-2017, 06:21 AM
Last Post: heiner55
  changing variable outside of a function tkj80 8 7,173 Jan-05-2017, 03:52 AM
Last Post: tkj80

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020