469,926 Members | 1,728 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,926 developers. It's quick & easy.

TypeError: unsubscriptable object.

Hello Everybody...

I have a problem..

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

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

def pushP(self,obj):
self.stackP.append(obj)

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

def popP(self):
rval=0
rval=self.stackP.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.stackManagement(expr.left)
self.stackManagement(expr.right)
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.stackManagement(0)
else:
self.popF(self.stackF)
self.stackManagement(mid*top)
if op == '+':
if top.__class__==V:
self.a+= self.peekF()

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

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

self.stackManagement(0)
else:
self.popF(self.stackF)
self.stackManagement(top/mid)
else:
self.a['rhs'] += self.popP(self.stackP)*self.popF(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.stackManagement(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.stackManagement(expr.left)
File "generation.py", line 176, in stackManagement
self.stackManagement(expr.right)
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
Jul 18 '05 #1
2 3280

"Balaji" <ba****@email.arizona.edu> wrote in message
news:49**************************@posting.google.c om...
class Stack:

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

See the potential problem? (If not, add
'print type(self.stackF)' to the top of this function.)
def stackManagement(self,expr): .... self.a+= self.peekF()
which now seems quite possible.
.... s.stackManagement(e1)
----------------------------------------------
I'm getting the following error TypeError: unsubscriptable object...
which means you tried to subscript an unsubscriptable object
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "generation.py", line 175, in stackManagement
self.stackManagement(expr.left)
File "generation.py", line 176, in stackManagement
self.stackManagement(expr.right)
File "generation.py", line 199, in stackManagement
self.a+= self.peekF()
File "generation.py", line 168, in peekF
rval= self.stackF[-1]


in the line immediately above.

Terry J. Reedy


Jul 18 '05 #2
In article <49**************************@posting.google.com >,
ba****@email.arizona.edu (Balaji) wrote:
Hello Everybody...

I have a problem.. [ ... ]----------------------------------------------
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.stackManagement(expr.left)
File "generation.py", line 176, in stackManagement
self.stackManagement(expr.right)
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...


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

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

and see what comes out.

Regards. Mel.
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Atul Kshirsagar | last post: by
reply views Thread by Luis P. Mendes | last post: by
5 posts views Thread by Randall Parker | last post: by
1 post views Thread by Gary Wessle | last post: by
9 posts views Thread by k.retheesh | last post: by
10 posts views Thread by Charles Russell | last post: by
2 posts views Thread by AWasilenko | last post: by
33 posts views Thread by christophertidy | last post: by
reply views Thread by Chris Rebert | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.