TypeError: unsubscriptable object.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Balaji

    TypeError: unsubscriptable object.

    Hello Everybody...

    I have a problem..

    This is the code...
    --------------------------------------
    class Stack:

    def __init__(self,e xpr):
    self.stackP=[]
    self.stackF=1
    self.a = {}
    # self.stackManag ement(expr)
    if not self.a.has_key( 'rhs'):
    self.a['rhs'] = 0
    # self.a['rhs'] += self.popP()*sel f.popF()

    def pushP(self,obj) :
    self.stackP.app end(obj)

    def pushF(self,obj) :
    stackF=stackF*o bj
    return stackF

    def popP(self):
    rval=0
    rval=self.stack P.pop()
    return rval

    def popF(self,obj):
    stackF/=obj
    return stackF

    def peekP(self):
    rval=0
    rval= self.stackP[-1]
    return rval
    def peekF(self):
    rval=0
    rval= self.stackF[-1]
    return rval
    def stackManagement (self,expr):

    #Determine the next symbol
    if expr.__class__= =E:
    self.pushP(expr .operator)
    self.stackManag ement(expr.left )
    self.stackManag ement(expr.righ t)
    else:
    self.pushP(expr )
    if len(self.stackP ) > 1:
    s = self.peekP()
    prev = self.stackP[-2]
    if prev in ('*','+','/','-'):
    if prev in ('*','/'):
    self.pushF(self .peekF()*s)
    else:#stackP[-3] must be an operator.
    top=self.popP()
    mid =self.popP()
    op=self.popP()
    if op == '*':
    if top.__class__== V:
    self.a += self.popF(self. stackF)

    self.stackManag ement(0)
    else:
    self.popF(self. stackF)
    self.stackManag ement(mid*top)
    if op == '+':
    if top.__class__== V:
    self.a+= self.peekF()

    self.stackManag ement(0)
    else:
    self.stackManag ement(mid+top)
    if op == '-':
    if top.__class__== V:
    self.a -= self.peekF()

    self.stackManag ement(0)
    else:
    self.stackManag ement(mid-top)
    if op == '/':
    if top.__class__== V:
    self.a /= self.popF(self. stackF)/(mid**2)

    self.stackManag ement(0)
    else:
    self.popF(self. stackF)
    self.stackManag ement(top/mid)
    else:
    self.a['rhs'] += self.popP(self. stackP)*self.po pF(self.stackF)

    and I'm giveing the following calls...
    ---------------------------

    from expression import *
    from generation import*

    (x,y,z) = (V(),V(),V()) # making instances of Variable class

    e1=x+y+z

    s=Stack(e1)

    s.stackManageme nt(e1)
    ----------------------------------------------
    I'm getting the following error TypeError: unsubscriptable object...
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "generation.py" , line 175, in stackManagement
    self.stackManag ement(expr.left )
    File "generation.py" , line 176, in stackManagement
    self.stackManag ement(expr.righ t)
    File "generation.py" , line 199, in stackManagement
    self.a+= self.peekF()
    File "generation.py" , line 168, in peekF
    rval= self.stackF[-1]
    -------------------------------------
    I 'm at my wit ends..

    Can anyone help...

    Thx in advance
  • Terry Reedy

    #2
    Re: TypeError: unsubscriptable object.


    "Balaji" <balaji@email.a rizona.edu> wrote in message
    news:494182a9.0 405211340.49873 a72@posting.goo gle.com...[color=blue]
    > class Stack:
    >
    > def __init__(self,e xpr):[/color]

    Note: tabs get eaten by some newsreaders.
    [color=blue]
    > self.stackP=[]
    > self.stackF=1[/color]
    ....[color=blue]
    > def peekF(self):
    > rval = 0 # useless line
    > rval= self.stackF[-1]
    > return rval[/color]
    # return self.stackF[-1] would be easier to read

    See the potential problem? (If not, add
    'print type(self.stack F)' to the top of this function.)
    [color=blue]
    > def stackManagement (self,expr):[/color]
    ....[color=blue]
    > self.a+= self.peekF()[/color]

    which now seems quite possible.
    ....[color=blue]
    > s.stackManageme nt(e1)
    > ----------------------------------------------
    > I'm getting the following error TypeError: unsubscriptable object...[/color]

    which means you tried to subscript an unsubscriptable object
    [color=blue]
    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > File "generation.py" , line 175, in stackManagement
    > self.stackManag ement(expr.left )
    > File "generation.py" , line 176, in stackManagement
    > self.stackManag ement(expr.righ t)
    > File "generation.py" , line 199, in stackManagement
    > self.a+= self.peekF()
    > File "generation.py" , line 168, in peekF
    > rval= self.stackF[-1][/color]

    in the line immediately above.

    Terry J. Reedy




    Comment

    • Mel Wilson

      #3
      Re: TypeError: unsubscriptable object.

      In article <494182a9.04052 11340.49873a72@ posting.google. com>,
      balaji@email.ar izona.edu (Balaji) wrote:[color=blue]
      >Hello Everybody...
      >
      >I have a problem..[/color]
      [ ... ][color=blue]
      >----------------------------------------------
      >I'm getting the following error TypeError: unsubscriptable object...
      >Traceback (most recent call last):
      > File "<stdin>", line 1, in ?
      > File "generation.py" , line 175, in stackManagement
      > self.stackManag ement(expr.left )
      > File "generation.py" , line 176, in stackManagement
      > self.stackManag ement(expr.righ t)
      > File "generation.py" , line 199, in stackManagement
      > self.a+= self.peekF()
      > File "generation.py" , line 168, in peekF
      > rval= self.stackF[-1]
      >-------------------------------------
      >I 'm at my wit ends..
      >
      >Can anyone help...[/color]

      Something has come unstuck. I would try wrapping
      the failing statement:

      try:
      rval= self.stackF[-1]
      except TypeError:
      print "TypeError, self.stackF:", repr(self.stack F)
      sys.exit (-1)

      and see what comes out.

      Regards. Mel.

      Comment

      Working...