0

I've declared a number of variables at the start of my script, as I'm using them in a number of different methods ("Functions" in python?). When I try to access them, I can't seem to get their value = or set them to another value for that matter. For example:

baseFile = open('C:/Users/<redacted>/Documents/python dev/ATM/Data.ICSF', 'a+')
secFile = open('C:/Users/<redacted>/Documents/python dev/ATM/security.ICSF', 'a+')

def usrInput(raw_input):
    if raw_input == "99999":
        self.close(True)
    else:
        identity = raw_input

def splitValues(source, string):
    if source == "ident":
        usrTitle = string.split('>')[1]
        usrFN = string.split('>')[2]
        usrLN = string.split('>')[3]
        x = string.split('>')[4]
        usrBal = Decimal(x)
        usrBalDisplay = str(locale.currency(usrBal))
    elif source == "sec":
        usrPIN = string.split('>')[1]
        pinAttempts = string.split('>')[2]

def openAccount(identity):
    #read all the file first. it's f***ing heavy but it'll do here.
    plString = baseFile.read()
    xList = plString.split('|')
    parm = str(identity)
    for i in xList:
        substr = i[0:4]
        if parm == substr:
            print "success"
            usrString = str(i)
        else:
            lNumFunds = lNumFunds + 1      
    splitValues("ident", usrString)

When I place baseFile and secFile in the openAccount method, I can access the respective files as normal. However, when I place them at the root of the script, as in the example above, I can no longer access the file - although I can still "see" the variable.

Is there a reason to this? For reference, I am using Python 2.7.

Wolfish
  • 960
  • 2
  • 8
  • 34
  • That's because baseFile and secFile are global variables. In order to modify them, you have to re-declare them inside the function as global baseFile. – blackmamba Oct 22 '15 at 14:09
  • @blackmamba So I can definitely modify them? Does this affect the original variable? I want to pass these variables between functions with their updated values. – Wolfish Oct 22 '15 at 14:13
  • @blackmamba that's not true. If you want to have a local variable to modify, you'd need to do that, but *of course* python allows you to modify state of the surrounding scope. – Marcus Müller Oct 22 '15 at 14:14
  • @MarcusMüller: I am not sure what you mean by "python allows you to modify state of the surrounding scope". – blackmamba Oct 22 '15 at 14:17
  • @Wolfish: Yes it does affect the original variable. Whenever you want to modify a global variable inside a function you have to re-declare it as global varname. But in case you are only reading them you can access it w/o redeclaring it. So you can get their updates values across functions. This link will give you better understanding : http://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them – blackmamba Oct 22 '15 at 14:21
  • @blackmamba So, my understanding is that I can use the `global` attribute, and pass updated values between functions, yes? – Wolfish Oct 22 '15 at 14:47
  • Yes. That's correct. – blackmamba Oct 22 '15 at 15:05

1 Answers1

0

methods ("Functions" in python?)

"function" when they "stand free"; "methods" when they are members of a class. So, functions in your case.

What you describe does definitely work in python. Hence, my diagnosis is that you already read something from the file elsewhere before you call openAccount, so that the read pointer is not at the beginning of the file.

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
  • Nope, I already thought of that. You can view the entire script here: http://pastebin.com/LSdYeR1G Note that the first function called is `login()`, which calls `openAccount()`. – Wolfish Oct 22 '15 at 14:10
  • However, is it possible that the problem lies with where I have put the first function call? – Wolfish Oct 22 '15 at 14:11
  • you're calling `openAccount` as many times as it takes to get a valid login; however, after the first attempt, the whole file has been read, so the read pointer is at the end of the file and you can't read it again! – Marcus Müller Oct 22 '15 at 14:14
  • I'm confused, I wrote the search function inside `openAccount()`, so I should only be calling it once? – Wolfish Oct 22 '15 at 14:15
  • but you call `openAccount()` multiple times, which contains the `read()`, right? After the first `read`, there will be nothing of the file left to read! – Marcus Müller Oct 22 '15 at 14:27
  • No, I only call it once - look at the `login()` function. If the login ID isn't recognised, it restarts the script, hence returning the `read()` pointer to 0, no? – Wolfish Oct 22 '15 at 14:35