1

How do I implement the Subclass B for the function getdata() to return the details in the constructor? I could achieve that if the constructor in B takes a single object, but having difficulty if the objects were two or more and passed as a list.

This example shows what I intended. Code lacks the full implementation.

class A(object):
    def __init__(self, a):
        self.a = a

    def geta(self):
        return str(self.a) 


class B(A):
    def __init__(self, b,c, [objA1, objA2]):
        self.b = b
        self.c = c
        super().__init__()
        # ...        

    def geta(self):
        return str(self.a) 

    def getb(self):
        return str(self.b)

    def getdata(self):
        return str(self.geta()) + str(self.getb()) + ...


obja1 = A('John')
obja2 = A('Doe')
obj = B('123', '456', [obja1, obja2])

# Test1 obj.getdata() == "John Doe 123 456"
K.Chams
  • 33
  • 5
  • um, is this Python 2?? Can you please provide a [mcve]? It's not really clear to me what you are asking. – juanpa.arrivillaga May 28 '20 at 10:39
  • "the objects were two or more and passed as a List." That's still just passing *a single object*, a `list` – juanpa.arrivillaga May 28 '20 at 10:40
  • Please fix your code, this does not work. Do you want to call `str(self.a)`? The `__init__` of `B` is invalid, etc. You should focus on understanding the basics first, before tackling inheritance. – Jan Christoph Terasa May 28 '20 at 10:41
  • You probably don't need inheritance for this, but you should really run your code yourself first to find out the problems it has, eg, How do you think that `geta()` should work? – quamrana May 28 '20 at 10:51
  • @JanChristophTerasa I didn't provide the complete [working] code. My intent in on the getdata function in Class B. Class A was indicated to know what I wanted to achieve. – K.Chams May 28 '20 at 11:10
  • @juanpa.arrivillaga Python 3. – K.Chams May 28 '20 at 11:11
  • You are in a hole and still digging! What do the `str(a)` and `str(b)` expressions mean apart from and error? – quamrana May 28 '20 at 11:38
  • @quamrana, I already stated the codes aren't working. It is the concept I want. I corrected that btw. Please what's your solution? – K.Chams May 28 '20 at 13:15

2 Answers2

1

You could do like this and pass as normal arguments and then convert into list

class B(A):
    def __init__(self,b,objA1,objA2):
        self.b = b
        self.list = [objA1, objA2]
        super().__init__()
obj = B("hello", "JOHN","DOE")

like this works aswell, basicaly u say that objs will be a list

class B(A):
     def __init__(self,b, objs: list):
         self.b = b
         self.list = objs
         super().__init__()
obj = B("hello", ["JOHN","DOE"])
Tiago Oliveira
  • 47
  • 1
  • 12
  • 'list' is not defined. By the way, obj = B("hello", ["JOHN","DOE"] must be obj = B("hello", [obja1,obja2]) – K.Chams May 28 '20 at 13:28
0

So you don't need inheritance here. Composition will be enough.

This code produces output according to your requirements:

class A:
    def __init__(self, a):
        self.a = a

    def __repr__(self):
        return str(self.a)


class B:
    def __init__(self, b, c, others):
        self.b = b
        self.c = c
        self.others = others

    def getdata(self):
        items = self.others + [self.b, self.c] 
        return ' '.join([str(item) for item in items])


obja1 = A('John')
obja2 = A('Doe')
obj = B('123', '456', [obja1, obja2])
print(obj.getdata())

Output:

John Doe 123 456
quamrana
  • 37,849
  • 12
  • 53
  • 71