Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python子类属性错误_Python_Python 3.x_Subclass_Computer Science_Subclassing - Fatal编程技术网

Python子类属性错误

Python子类属性错误,python,python-3.x,subclass,computer-science,subclassing,Python,Python 3.x,Subclass,Computer Science,Subclassing,在今天的讲座中,我们开始在Python中使用子类。例如,我们得到的代码类似于一个非常基本的社交网络,如下所示: class socialNetwork: class node: def __init__(self, name, friendList): self.name=name self.friendList=friendList def __init__(self): self

在今天的讲座中,我们开始在Python中使用子类。例如,我们得到的代码类似于一个非常基本的社交网络,如下所示:

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)
但是,每当我尝试运行此操作时,都会收到以下消息:

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'
回溯(最近一次呼叫最后一次):
**空闲内部异常:
文件“C:\Users\Mike\AppData\Local\Programs\Python\Python36-
32\lib\idlelib\run.py”,第460行,运行代码
exec(代码,self.locals)
文件“C:/Users/Mike/AppData/Local/Programs/Python/Python36-32/run.py”,
第15行,在
s、 addPerson(“约翰”、“爱丽丝”、“鲍勃”)
AttributeError:“socialNetwork”对象没有属性“addPerson”

简单地说,我不知道为什么会遇到这个错误,尤其是在教授很好地运行了相同的代码之后。我是否遗漏了什么,如果遗漏了,请有人指出吗?

您还没有定义任何子类。在Python中,通过将父类放在括号中来指定继承,例如:

class Node:
    pass

class Leaf(Node):
    # Leaf is a subclass of Node
    pass
“Network”和“Node”作为子类并没有什么意义,但其中一个应该属于另一个


您所做的是使用一个属性定义一个类
socialNetwork
,一个名为
node
的类。这就是为什么您会得到一个
AttributeError
,因为
socialNetwork

中没有
addPerson
属性,因为您的类缩进错误,所以您的类没有
addPerson
方法。应该是这样的:

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)

缩进在
python
中很重要。出现问题的一个线索是,您在同一级别上有两个
\uuu init\uuuu
方法。

首先,
node
不是
socialNetwork
的子类,而是嵌套在后者中的类。
其次,
socialNetwork
实际上没有属性
addPerson
,但是
socialNetwork.node
有属性。

您确定此代码正确吗?每个类应该只有一个
\uu init
方法。这里的
node
有两个,而
socialNetwork
两者都没有。这不是一个嵌套类的子类,在这里没有真正意义。。。