variable scope

G

gonzlobo

Greetings,
I've been using Python to successfully parse files. When the entire
program was smaller, the variable firstMsg worked fine, but now
doesn't because it's used in function PID_MinMax. I know it's a result
of variables and their scope.

I declare the variable 'firstMsg = 0' in the main loop, pass it to the
function 'PID_MinMax(firstMsg, PID)'. When the function increments the
variable, it doesn't pass it back to the main program. What am I doing
wrong?


---- major snippage) ---
firstMsg = 0
skipHeader = 13

pPIDs = ['0321'] # hundreds more
pLen = len(pPIDs)
pMsgNum = pLen * [0]
pMax = pLen * [0]
pMin = pLen * [10]
pLast = pLen * [0]


def PID_MinMax(firstMsg, PID):
idx = pPIDs.index(PID)
pMsgNum[idx] += 1
# Need to have 2 samples to determine Delta
if firstMsg != 0:
tDelta = tCurrent - pLast[idx]
if tDelta > pMax[idx]:
pMax[idx] = tDelta
if tDelta < pMin[idx]:
pMin[idx] = tDelta
elif firstMsg == 0:
firstMsg = 1
pLast[idx] = tCurrent
print pMin, pMax
return firstMsg


############## main ##############
bf_file = file('bf_data/sByteflightLog_wISS.txt', 'r')
for line in bf_file:
# skip header
if skipHeader != 0:
skipHeader -= 1
# skip footer
elif line == '\n':
break
else:
raw_msg = line.split()
tCurrent = int(raw_msg[0], 16) * 0.0001
PID = raw_msg[2]

if PID in pPIDs:
PID_MinMax(firstMsg, PID)
 
B

Bruno Desthuilliers

gonzlobo a écrit :
Greetings,
I've been using Python to successfully parse files. When the entire
program was smaller, the variable firstMsg worked fine, but now
doesn't because it's used in function PID_MinMax. I know it's a result
of variables and their scope.

I declare the variable 'firstMsg = 0' in the main loop, pass it to the
function 'PID_MinMax(firstMsg, PID)'. When the function increments the
variable, it doesn't pass it back to the main program. What am I doing
wrong?


---- major snippage) ---
firstMsg = 0 (snip)
def PID_MinMax(firstMsg, PID): (snip)
if firstMsg != 0: if firstMsg:
tDelta = tCurrent - pLast[idx]
if tDelta > pMax[idx]:
pMax[idx] = tDelta
if tDelta < pMin[idx]:
pMin[idx] = tDelta
elif firstMsg == 0: else:
firstMsg = 1
pLast[idx] = tCurrent
print pMin, pMax
return firstMsg
############## main ############## (snip)
if PID in pPIDs:
PID_MinMax(firstMsg, PID)
firstMsg = PID_MinMax(firstMsg, PID)

You're returning firstMsg from PID_MinMax, but discarding the value...
 
B

Bruno Desthuilliers

gonzlobo a écrit :
<ot>
Please keep this on clpy...
Sorry, but I don't understand. I *should* pass firstMsg to the
function like I did (PID_MinMax(firstMsg)), correct?
Yes.

Then I should
pass the variable back to the main loop by 'return firstMsg', correct?

s/variable/value/

Yes, you have to return the value from your function. But then, if you
don't store this value somewhere, it's lost. What you have to understand
is that you actually have *2* variables named 'firstMsg' : one in the
'main loop', and one in PID_MinMax(). Rebinding the second name has no
impact on the first. So you have to explicitely assign the return value
of PID_MinMax() to the main loop's firstMsg variable. Else, this value
is simply discarded.

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top