I am building up a tree and a dictionary mapping nodes in the tree to unique ids. When trying to access objects in the dictionary I am experiencing some unexpected behaviour. I am able to access some inherited attributes of the object, but not other. I've extracted part of the project and modified it such that it is hopefully understandable:
#!/usr/bin/env python
IMPORTS = vars()
class Node(object):
SYMTAB = {}
def __init__(self, kwargs={}):
self.ati = kwargs.get(u'@i')
self._add_symbol(self.ati, self)
self.atr = kwargs.get(u'@r')
def _add_symbol(self, k, v):
self.SYMTAB[k] = v
class CompilationUnit(Node):
def __init__(self, kwargs={}):
super(CompilationUnit, self).__init__(kwargs)
self.types = map(lambda x: IMPORTS[x['@t']](x),
kwargs.get('types').get('@e', []))
class BodyDeclaration(Node):
def __init__(self, kwargs={}):
super(BodyDeclaration, self).__init__(kwargs)
class TypeDeclaration(BodyDeclaration):
def __init__(self, kwargs={}):
super(TypeDeclaration, self).__init__(kwargs)
self.members = map(lambda x: IMPORTS[x[u'@t']](x),
kwargs.get(u'members').get(u'@e', []))
class ClassOrInterfaceDeclaration(TypeDeclaration):
def __init__(self, kwargs={}):
super(ClassOrInterfaceDeclaration, self).__init__(kwargs)
class FieldDeclaration(BodyDeclaration):
def __init__(self, kwargs={}):
super(FieldDeclaration, self).__init__(kwargs)
print '*'*10, 'SYMTAB:'
for k,v in self.SYMTAB.items():
print k,v
print '*'*10
print 'SYMTAB[self.atr]:',self.SYMTAB[self.atr]
print self.SYMTAB[self.atr].atr
print self.SYMTAB[self.atr].members
d = {u'@i': 0, u'@r': None, u'@t': u'CompilationUnit', 'types':
{u'@e':
[{u'@t': u'ClassOrInterfaceDeclaration', u'@i': 1, u'@r': 0,
u'members':
{u'@e':
[{u'@t': 'FieldDeclaration', u'@i': 2, u'@r': 1}]}}]}}
c = CompilationUnit(d)
print c
This will produce the following output:
********** SYMTAB:
0 <__main__.CompilationUnit object at 0x105466f10>
1 <__main__.ClassOrInterfaceDeclaration object at 0x10547c050>
2 <__main__.FieldDeclaration object at 0x10547c150>
**********
SYMTAB[self.atr]: <__main__.ClassOrInterfaceDeclaration object at 0x10547c050>
0
Traceback (most recent call last):
File "class_error.py", line 74, in <module>
c = CompilationUnit(d)
File "class_error.py", line 30, in __init__
kwargs.get('types').get('@e', []))
File "class_error.py", line 29, in <lambda>
self._types = map(lambda x: IMPORTS[x['@t']](x),
File "class_error.py", line 54, in __init__
super(ClassOrInterfaceDeclaration, self).__init__(kwargs)
File "class_error.py", line 45, in __init__
kwargs.get(u'members').get(u'@e', []))
File "class_error.py", line 44, in <lambda>
self._members = map(lambda x: IMPORTS[x[u'@t']](x),
File "class_error.py", line 65, in __init__
print self.SYMTAB[self.atr].members
AttributeError: 'ClassOrInterfaceDeclaration' object has no attribute 'members'
I'm not even sure where to begin trying to fix this. Recently I added the print self.SYMTAB[self.atr].atr
line and saw that this actually worked. The only thing I can think of is that FieldDeclaration
doesn't inherit from TypeDeclaration
, which is where the members
attribute is actually defined. But why should this matter? I am accessing the ClassOrInterfaceDeclaration
node, which does inherit from TypeDeclaration
? This object should have a members
attribute. What am I missing about how to access this attribute?
There seem to multiple other problems in your program but here:
class TypeDeclaration(BodyDeclaration):
def __init__(self, kwargs={}):
super(TypeDeclaration, self).__init__(kwargs)
self.members = map(lambda x: IMPORTS[x[u'@t']](x),
kwargs.get(u'members').get(u'@e', []))
the part:
kwargs.get(u'members')
tries to access self.members
which you are just about to create.
You put u'@t': u'ClassOrInterfaceDeclaration'
into d
. Then in here:
self.members = map(lambda x: IMPORTS[x[u'@t']](x),
kwargs.get(u'members').get(u'@e', []))
You try access ClassOrInterfaceDeclaration().members
, which you are just about to create. Instanziation of ClassOrInterfaceDeclaration
calls the __init__()
of TypeDeclaration
that contains the cod above.
You need to let all __init__()
functions to finish before trying to access members. For example, you can move all your printing into a own method:
class FieldDeclaration(BodyDeclaration):
def __init__(self, kwargs={}):
super(FieldDeclaration, self).__init__(kwargs)
def meth(self):
print '*'*10, 'SYMTAB:'
for k,v in self.SYMTAB.items():
print k,v
print '*'*10
print 'SYMTAB[self.atr]:',self.SYMTAB[self.atr]
print self.SYMTAB[self.atr].atr
print self.SYMTAB[self.atr].members
d = {u'@i': 0, u'@r': None, u'@t': u'CompilationUnit', 'types':
{u'@e':
[{u'@t': u'ClassOrInterfaceDeclaration', u'@i': 1, u'@r': 0,
u'members':
{u'@e':
[{u'@t': 'FieldDeclaration', u'@i': 2, u'@r': 1}]}}]}}
c = CompilationUnit(d)
print c
c.SYMTAB[2].meth()
Output:
<__main__.CompilationUnit object at 0x104ef9510>
********** SYMTAB:
0 <__main__.CompilationUnit object at 0x104ef9510>
1 <__main__.ClassOrInterfaceDeclaration object at 0x104ef9610>
2 <__main__.FieldDeclaration object at 0x104ef9710>
**********
SYMTAB[self.atr]: <__main__.ClassOrInterfaceDeclaration object at 0x104ef9610>
0
[<__main__.FieldDeclaration object at 0x104ef9710>]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.