> While trying to implement a PE parser, I ran into the following problem: > > #************** START CODE******************* > data = file.read(128); > directoryTable = struct.unpack('LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL', data); > i=0; > print "Export table 0x%08X + 0x%08x" % (directoryTable[i+=1], > directoryTable[i+=1]); > print "Import table 0x%08X + 0x%08x" % (directoryTable[i+=1], > directoryTable[i+=1]); > #************** END CODE******************* > > This code throws a syntax error at the first i+=1 on "line 4".
Hi Joe, Yes. Python's assignments aren't expressions --- in Python, assignments are meant to visually stand out. Unfortunately, this means you can't put the assignment within the array indexing expression. There are a few workarounds. One is to treat the directoryTable as a stream of values that we can iterate across. For example: ################################## >>> values = (3, 1, 4, 1, 5) >>> i = iter(values) >>> i <tupleiterator object at 0x6dd50> ################################## 'i' here is an "iterator" that we can repeatedly use to get sequential elements: ############# >>> i.next() 3 >>> i.next() 1 >>> i.next() 4 ############# In some sense, this should allow you to do what you had in your original code, since i.next() will both give you the value and, internally, move the iterator forward. See: http://www.python.org/doc/tut/node11.html#SECTION0011900000000000000000 for a quick-and-dirty introduction to iterators. Best of wishes! _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor