Class instantiation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Colin J. Williams

    #1

    Class instantiation

    In the example below, with the disassembly following that, we run into
    trouble with the line:
    self.connect(fi leID, mode= 'r') # open sheet in the read mode

    the traceback is:
    Traceback (most recent call last):
    File "C:\Documen ts and Settings\cjw\My Documents\OODev \tArray.py",
    line 26, in __init__
    self.connect(fi leID, mode= 'r') # open sheet in the read mode
    NameError: global name 'fileID' is not defined

    At line 26, location 31, why is LOAD_GLOBAL generated for fileId, when
    LOAD_FAST has done the job at locations 0 and 20?

    I would appreciate advice.

    Colin W.

    class arSpread(object ):
    def __init__(self, fileId= None, ar= None):
    if fileId:
    if ar is not None:
    print fileId
    self.connect(fi leID, mode= 'r') # open sheet in the read mode
    else:
    self.connect(fi leID, mode= 'w') # open the sheet in the
    write mode
    if ar is not None:
    self.setArray(a r)



    [Dbg]>>dis.disassemb le(arSpread.__i nit__.im_func.f unc_code)
    23 0 LOAD_FAST 1 (fileId)
    3 JUMP_IF_FALSE 64 (to 70)
    6 POP_TOP

    24 7 LOAD_FAST 2 (ar)
    10 LOAD_GLOBAL 2 (None)
    13 COMPARE_OP 9 (is not)
    16 JUMP_IF_FALSE 28 (to 47)
    19 POP_TOP

    25 20 LOAD_FAST 1 (fileId)
    23 PRINT_ITEM
    24 PRINT_NEWLINE

    26 25 LOAD_FAST 0 (self)
    28 LOAD_ATTR 4 (connect)
    31 LOAD_GLOBAL 5 (fileID)
    34 LOAD_CONST 1 ('mode')
    37 LOAD_CONST 2 ('r')
    40 CALL_FUNCTION 257
    43 POP_TOP
    44 JUMP_ABSOLUTE 71
    > 47 POP_TOP
Working...