469,628 Members | 1,192 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,628 developers. It's quick & easy.

variable scope

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)
Jan 18 '07 #1
2 883
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...
Jan 18 '07 #2
gonzlobo a écrit :
<ot>
Please keep this on clpy...
</ot>
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
Jan 19 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Grant Wagner | last post: by
4 posts views Thread by Gery D. Dorazio | last post: by
23 posts views Thread by Russ Chinoy | last post: by
pbmods
1 post views Thread by pbmods | last post: by
2 posts views Thread by Shraddha | last post: by
112 posts views Thread by istillshine | last post: by
3 posts views Thread by SRoubtsov | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.