Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Questions on variable changing between files.
(Apr-24-2017, 07:04 AM)Sello Wrote:
# This is File A
from fileb import bvar

bvar = 10

There seem to be a lot of assumptions that you want to save that value to the original file, so the next time you open up fileb, it'd be 10 instead of whatever it used to be.  I'm not sure that's what you're asking.  If you just want to change it while it's running, sort of like a config value, then work with the file as a module directly, instead of only importing the single variable.  Like so:

#file a
import fileb

print(fileb.bvar) # 5, maybe

fileb.bvar = 10

print(fileb.bvar) # 10 here, and also 10 for anything in fileb that uses it
Although you'd need a pretty good reason to want to do that, since it could easily make reading fileb and understanding why the output is as it is fairly complicated.
That doesn't physically change bvar on the main file for it. If another file needs to access that variable, it will not be 10, but 0 as that's the value on the file that holds it. I think I explained my first post badly, so I'll use a better example.

Say you win a card game 5 times. Each time you win, this variable increments by 1 and is stored/saved to another file. When the variable is accessed again, it'll display that you won 5 times.

I'm total beginner at python(and is the only programming language I have begun to learn), only started 6 days ago, it is possible I am getting slightly ahead of myself and should take a step back.
As I said, you have to load the file, manipulate the data and store all back.
After your last post, I think that you need some database. For instance sqlite3. It is serverless database but for big projects look at PostgreSQL
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  how write variable in files and retreive it frank 2 309 Jan-30-2019, 12:53 AM
Last Post: frank
  int(variable) not changing variable to an integer StoopidChicken 2 837 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 2,454 Nov-25-2017, 06:21 AM
Last Post: heiner55
  changing variable outside of a function tkj80 8 2,684 Jan-05-2017, 03:52 AM
Last Post: tkj80

Forum Jump:

Users browsing this thread: 1 Guest(s)