简体   繁体   English

Python子类属性错误

[英]Python Subclass Attribute Error

During a lecture today we began to do work with subclasses within Python. 在今天的一次演讲中,我们开始在Python中处理子类。 As an example, we were given code resembling a very basic social network which is as follows: 例如,我们得到的代码类似于一个非常基本的社交网络,如下所示:

class socialNetwork:
    class node:
        def __init__(self, name, friendList):
            self.name=name
            self.friendList=friendList

        def __init__(self):
            self.nodeList=[]

        def addPerson(self, name, friendList):
            person=self.node(name,friendList)
            self.nodeList.append(person)

s = socialNetwork()
s.addPerson("John",["Alice","Bob"])
s.addPerson("Alice",["John","Bob","Jeff"])
s.addPerson("Bob",["John","Alice","Jeff","Ken"])
s.addPerson("Jeff",["Alice","Bob","Barbra"])
s.addPerson("Ken",["Bob","Barbra"])
s.addPerson("Barbra",["Jeff","Ken"])
for person in s.nodeList:
    print("name: ",person.name, "\n\t friends: ",person.friendList)

However, whenever I attempt to run this, I receive the following message: 但是,每当我尝试运行此命令时,都会收到以下消息:

Traceback (most recent call last):
** IDLE Internal Exception: 
  File "C:\Users\Mike\AppData\Local\Programs\Python\Python36-
32\lib\idlelib\run.py", line 460, in runcode
    exec(code, self.locals)
  File "C:/Users/Mike/AppData/Local/Programs/Python/Python36-32/run.py", 
line 15, in <module>
    s.addPerson("John",["Alice","Bob"])
AttributeError: 'socialNetwork' object has no attribute 'addPerson'

Simply put, I have no idea why I am encountering this error, especially after the professor ran the same code just fine. 简而言之,我不知道为什么会遇到此错误,尤其是在教授运行相同的代码之后。 Am I missing something here, and if so could someone please point it out? 我在这里错过什么吗?如果可以,请指出吗?

Your class doesn't have addPerson method, because your class is indented wrong way. 您的班级没有addPerson方法,因为您的班级缩进方式错误。 It should look like this: 它看起来应该像这样:

class socialNetwork:
    class node:
        def __init__(self, name, friendList):
            self.name=name
            self.friendList=friendList

    def __init__(self):
        self.nodeList=[]

    def addPerson(self, name, friendList):
        person=self.node(name,friendList)
        self.nodeList.append(person)

Indentation does matter in python . 缩进在python确实很重要。 A clue that something is wrong would be you having two __init__ methods at the same level. 如果您在同一级别上有两个__init__方法,则可能是出现问题的线索。

You haven't defined any subclasses. 您尚未定义任何子类。 Inheritance is specified in Python by putting the parent class(es) in parenthesis, eg: 在Python中,通过将父类放在括号中来指定继承,例如:

class Node:
    pass

class Leaf(Node):
    # Leaf is a subclass of Node
    pass

"Network" and "Node" don't really make sense to be subclasses, but one should be composed of the other. “网络”和“节点”作为子类并没有什么意义,但其中一个应该另一个组成

What you've done is define a class socialNetwork with one attribute, a class called node . 您要做的是定义一个带有一个属性的类socialNetwork ,一个名为node的类。 That's why you get an AttributeError , because there is no addPerson attribute in socialNetwork . 这就是为什么出现AttributeError的原因,因为在socialNetwork没有addPerson属性。

Firstly, node is not a subclass of socialNetwork but a class nested within the latter. 首先, node不是socialNetwork的子类,而是嵌套在后者中的类。 Secondly, socialNetwork actually has no attribute addPerson , but socialNetwork.node does. 其次, socialNetwork实际上没有属性addPerson ,而socialNetwork.node有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM