从另一个 class 获取对象

get object from another class

我试图从 class B 在 class A 中获取一个对象,因为 class A 由 class B 的对象组成:

ObjectClassB = [ObjectCLassA1,ObjectClassA2,ObjectClassA3] 

with ObjectClassA 是 Class B 的子列表。

使用此代码,我试图将一个元素添加到 Class B 的一个对象以获得此结果:

[[1, 2, 3], [3, 4, 5], [6,7,8]]
class B: 
    def __init__(self,X,Y,Z):
        self.X=X
        self.Y=Y
        self.Z=Z
    def Xreturner(self):
        return self.X
    def Yreturner(self):
        return self.Y
    def Zreturner(self):
        return self.Z

class A: # the class A is a composed from many object of B
    def __init__(self):
        self.ls=[]
        self.lst=[[1,2,3],[3,4,5]] # 
    def __str__(self):
        return str(self.lst)
    def __repr__(self):
        return str(self.__dict__) 

    def add(self,X,Y,Z): # trying to add b object to the list
        b=B(X,Y,Z)
        print(b)
        self.ls.append(b)
        self.lst.append(self.ls)
        print(self.lst)
#### TEST####
objA=A()
objA.add(6,7,8)

当我执行这个测试时,我得到 print(b):

>>> <__main__.B instance at 0x1d8ab90>

print(self.lst):

>>> [[1, 2, 3], [3, 4, 5], [...]]

我该如何解决这个问题?

问题在于您正在尝试 print 一个对象。 print(b) 将打印对象在内存中的地址,因为它无法推断应该打印什么。

class B: 
    def __init__(self,X,Y,Z):
        self.X=X
        self.Y=Y
        self.Z=Z
    def Xreturner(self):
        return self.X
    def Yreturner(self):
        return self.Y
    def Zreturner(self):
        return self.Z
    def listReturner(self):
        return [self.X, self.Y, self.Z] #return a list ready to be added

class A: # the class A is a composed from many object of B
    def __init__(self):
        self.ls=[]
        self.lst=[[1,2,3],[3,4,5]] # 
    def __str__(self):
        return str(self.lst)
    def __repr__(self):
        return str(self.__dict__) 

    def add(self,X,Y,Z): # trying to add b object to the list
        b=B(X,Y,Z)
        print(b) #Will print the address of b
        print(b.X) #Will print the value of X in b
        self.ls.append(b)

        self.ls.append([b.X, b.Y, b.Z]) ### EDIT: this will add the desired values to the list

        print(self.ls) #Will print the addresses of the objects in the list
        print([a.X for a in self.ls]) #Will print the values of X for each object in the list
        self.lst.append(self.lst) #This line might be wrong
        print(self.lst)
#### TEST####
objA=A()
objA.add(6,7,8)

正如所指出的那样,您的代码(或多或少:下面有更多内容)正在做正确的事情,您真正需要解决的只是 class B 呈现 的方式印刷后向世界展示自己。为此,您可以向 class B 添加一个 __repr__ 方法,这将解决该问题。

我看到的另一个问题是 self.lst.append(self.ls),它将附加到列表 lst 中的列表。我想你可以跳过那个,只需 运行 self.lst.append(b) 就可以了。

请注意,class Aself.lst 中的前两个元素 不是 类型 class B,但我想那是你希望它如何工作。

代码在这里,也看到它在行动:https://eval.in/671341

class B: 
    def __init__(self,X,Y,Z):
        self.X=X
        self.Y=Y
        self.Z=Z
    def Xreturner(self):
        return self.X
    def Yreturner(self):
        return self.Y
    def Zreturner(self):
        return self.Z
    def __repr__(self):
        return str([self.X, self.Y, self.Z])


class A: # the class A is a composed from many object of B
    def __init__(self):
        self.ls=[]
        self.lst=[[1,2,3],[3,4,5]] # 
    def __str__(self):
        return str(self.lst)
    def __repr__(self):
        return str(self.__dict__) 

    def add(self,X,Y,Z): # trying to add b object to the list
        b=B(X,Y,Z)
        print(b)
        # self.ls.append(b)         # b will be represented as list
        # self.lst.append(self.ls)  # double append: let's not do that
        self.lst.append(b)          # simply append the object
        print(self.lst)
#### TEST####
objA=A()
objA.add(6,7,8)

这是您想要的最小示例。

B

上实施 __repr__
class B: 
    def __init__(self,X,Y,Z):
        self.X=X
        self.Y=Y
        self.Z=Z

    def __repr__(self):
        return str([self.X, self.Y, self.Z])

b1 = B(1, 2, 3)
b2 = B(3, 4, 5)

print(b1)  # [1, 2, 3]

b_list = [b1, b2]

print(b_list) # [[1, 2, 3], [3, 4, 5]]