访问超类中的属性

时间:2016-03-12 17:32:33

标签: python python-2.7 inheritance

我正在构建一个树和一个字典,将树中的节点映射到唯一ID。当试图访问字典中的对象时,我遇到了一些意外的行为。我能够访问该对象的一些继承属性,但不能访问其他属性。我已经提取了项目的一部分并进行了修改,以便它可以理解:

#!/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

这将产生以下输出:

********** 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'

我甚至不确定从哪里开始尝试解决这个问题。最近我添加了print self.SYMTAB[self.atr].atr行,看到这确实有效。我唯一能想到的是FieldDeclaration不会从TypeDeclaration继承,而members属性实际上是ClassOrInterfaceDeclaration属性的定义。但为什么这个问题呢?我正在访问TypeDeclaration节点,它继承自members?该对象应具有set /p属性。关于如何访问此属性,我缺少什么?

1 个答案:

答案 0 :(得分:-1)

问题

您的计划中似乎存在多个其他问题,但在此处:

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', []))

部分:

kwargs.get(u'members')

尝试访问您即将创建的self.members

您将u'@t': u'ClassOrInterfaceDeclaration'放入d。然后在这里:

        self.members = map(lambda x: IMPORTS[x[u'@t']](x),
                        kwargs.get(u'members').get(u'@e', []))

您尝试访问即将创建的ClassOrInterfaceDeclaration().membersClassOrInterfaceDeclaration的Instanziation调用包含上述鳕鱼的__init__() TypeDeclaration

解决方案

在尝试访问成员之前,您需要完成所有__init__()个函数。例如,您可以将所有打印移动到自己的方法中:

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()

输出:

<__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>]