[英]Accessing attributes in superclass
I am building up a tree and a dictionary mapping nodes in the tree to unique ids.我正在构建一棵树和一个字典,将树中的节点映射到唯一的 id。 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.最近我添加了
print self.SYMTAB[self.atr].atr
行,发现这确实有效。 The only thing I can think of is that FieldDeclaration
doesn't inherit from TypeDeclaration
, which is where the members
attribute is actually defined.我唯一能想到的是
FieldDeclaration
不继承自TypeDeclaration
,这是members
属性实际定义的地方。 But why should this matter?但这有什么关系呢? I am accessing the
ClassOrInterfaceDeclaration
node, which does inherit from TypeDeclaration
?我正在访问继承自
TypeDeclaration
的ClassOrInterfaceDeclaration
节点? This object should have a members
attribute.这个对象应该有一个
members
属性。 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.尝试访问您即将创建的
self.members
。
You put u'@t': u'ClassOrInterfaceDeclaration'
into d
.你把
u'@t': u'ClassOrInterfaceDeclaration'
放入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.您尝试访问您即将创建的
ClassOrInterfaceDeclaration().members
。 Instanziation of ClassOrInterfaceDeclaration
calls the __init__()
of TypeDeclaration
that contains the cod above.的Instanziation
ClassOrInterfaceDeclaration
调用__init__()
的TypeDeclaration
包含上述鳕鱼。
You need to let all __init__()
functions to finish before trying to access members.在尝试访问成员之前,您需要让所有
__init__()
函数完成。 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>]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.