Python:对象没有属性错误:数组

Şev*_*man 2 python arrays class

class foo:
    #initially
    def __init__(self):
        self.nodes1=[]
        self.nodes2=[]
        self.edges=[]

    def add_node(self,type,x,y):
        if type==1:
            self.nodes1.append(node1(x,y))


class node1:
    def __init__(self,x,y): #Getting coordinates from outside while creating the class.
        self.x=x
        self.y=y


b_foo = foo 
b_foo.add_node(b_foo,1,0,1)
Run Code Online (Sandbox Code Playgroud)

我尝试在类的数组中添加一个元素.此代码给出如下错误:

AttributeError:类型对象'bipartite'没有属性'nodes1'

Mik*_*tty 7

您应该创建一个类的实例:

b_foo = foo() # creates a class instance
b_foo.add_node(1,0,1) # "self" is passed implicitly
Run Code Online (Sandbox Code Playgroud)