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

标签 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)

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

AttributeError: type object 'bipartite' has no attribute 'nodes1'

最佳答案

你应该为你的类创建一个实例:

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

关于Python:对象没有属性错误:一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52478741/

相关文章:

PHP 数组,将数组项的计数递归附加到数组

python - 在类中创建 tkinter 标签、按钮等

c++ - 如何将 void 指针转换为类数组

python - 带有python请求库的zomato api请求

java - 无法将数组传递到方法中

python - Pandas /Python : How to create new column based on values from other columns and apply extra condition to this new column

Javascript 数组//从指定索引开始对数组重新排序,同时保持原始顺序

c++ - 前向声明模板类时无效使用不完整类型

python - 使用 selenium webdriver 等待元素的属性更改值

python - 如何使用字面量来分隔一个或多个单词?