我试图在另一个函数中使用一个函数并给出错误。我使用类作为结构。我尝试将块初始化为零,但它没有用。块也是一个字节
Chunk = bytearray(b'\x00') # is it a good idea to use this or any other options?
class Prog:
PROGRAMMING_FLAG = False
SET_ADD_FLAG = False
DATA_FLAG = False
ADD_COMMAND_FLAG = False
ERASE_FLAG = False
ATTEMPT = 1
def prog_flash(Chunk):
try:
Prog.DATA_BUFFER = []
Prog.ADD_COMMAND_FLAG = True
Prog.DATA_BUFFER.append(hex(0x02))
No_Of_Bytes = Chunk * 64
for index in range(0,64):
Prog.DATA_BUFFER.append(hex(DataBuffer[No_Of_Bytes+index]))
Prog.DATA_BUFFER.append(hex(Calculate_Checksum(Prog.DATA_BUFFER[1:Prog.DATA_BUFFER.__len__()])))
Prog.DATA_FLAG = True
return Prog.DATA_BUFFER
except Exception, e:
print("Error")
pass
def other_fn():
prog_flash(Chunk)
错误:
NameError:未定义全局名称“Chunk”
答案 0 :(得分:0)
未定义DataBuffer,也未定义Calculate_Checksum,
结构应该像
Chunk = bytearray(b'\x00') # is it a good idea to use this or any other options?
class Prog:
PROGRAMMING_FLAG = False
SET_ADD_FLAG = False
DATA_FLAG = False
ADD_COMMAND_FLAG = False
ERASE_FLAG = False
ATTEMPT = 1
def prog_flash(Chunk):
try:
Prog.DATA_BUFFER = []
Prog.ADD_COMMAND_FLAG = True
Prog.DATA_BUFFER.append(hex(0x02))
No_Of_Bytes = Chunk * 64
for index in range(0,64):
Prog.DATA_BUFFER.append(hex(DataBuffer[No_Of_Bytes+index]))
Prog.DATA_BUFFER.append(hex(Calculate_Checksum(Prog.DATA_BUFFER[1:Prog.DATA_BUFFER.__len__()])))
Prog.DATA_FLAG = True
return Prog.DATA_BUFFER
except Exception, e:
# handle Expection
print "There was an error: %s" %(e)
pass
def other_fn():
prog_flash(Chunk)
if __name__ == '__main__':
other_fn()