验证我对此python代码段的解释是否正确

Jas*_*son 5 python code-snippets

我正在分析一组python脚本并遇到了这个片段.我不确定我的解释是否正确,因为我没有遇到任何类似的C或Java代码而且我不懂Python.

for i in xrange(self.num_sections):
        offset, a1,a2,a3,a4 = struct.unpack('>LBBBB', self.data_file[78+i*8:78+i*8+8])
        flags, val = a1, a2<<16|a3<<8|a4
        self.sections.append( (offset, flags, val) )
Run Code Online (Sandbox Code Playgroud)

我的解释是这样的:

for each item in num_sections
  convert the data_file range into a big-endian unsigned long, and 4 unsigned char
  insert unpacked values into offset, a1, a2, a3 and a4 variables

  set flags to = a1
  set val to a2 shifted left 16 bits then OR'd with a3 shifted right 8 bits 
  then OR'd with a4
Run Code Online (Sandbox Code Playgroud)

本质上,我认为原始的解包操作提取8个字节,将其中的4个转储为无符号长整数,然后将其余的按顺序添加到a*变量中.

Jon*_*ard 0

是的,你的解释是正确的。