0

I'm supposed to create three classes: a parent, and child 1 and child 2.
Child 1 and 2 are supposed to inherit from the Parent class. So I believe I've done that.

class Parent:
    """Parent Object"""

    def __init__(self):
        self.greeting = "Hi I'm a Parent Object!"


class ChildA(Parent):

    def __init__(self):
        childclass.__init__(self)
        self.childgreeting = "Hi I'm a Child Object!"


class ChildB(Parent):
    pass

Now I have to write a parent object and the children objects which will print out their respective strings.
That's where I'm getting confused: I already put in the strings that they are a child or parent object within their classes.
But how do I get them to print as an object?

I've started out my code like this.

class Parent(object):

class ChildA(object):

class ChildB(object):

How to get those strings to print is bugging me.
And I have a feeling that my ChildA code for the class is not correct either.

Can anyone help me?

4
  • You mean the name of the class as a string? Commented Sep 24, 2016 at 0:39
  • If Parent is indeed a parent, then why do you have ChildA and ChildB inheriting from object? Commented Sep 24, 2016 at 0:44
  • By the way childclass.__init__ won't work since 'childclass' is not defined Commented Sep 24, 2016 at 0:46
  • It's supposed to be that Parent has a greeting attribute and childA has a greeting attribute and childB has "pass". After that I'm supposed to create three objects that will print their respective string attributes to the console! Commented Sep 24, 2016 at 1:02

2 Answers 2

1

Child 1 and 2 are supposed to inherit from the Parent class. So I believe I've done that

Yes, in the first code, you have, but not in the second code.

I have to write a parent object and child 1 and 2 objects that will print out their respective strings

Okay...

p = Parent()
child_a = ChildA()

print(p.greeting) 
print(child_a.childgreeting)

However - ChildA() won't work because __init__ should look like this

class ChildA(Parent):

    def __init__(self):
        super().__init__() # This calls the Parent __init__
        self.childgreeting = "Hi I'm a Child Object!"

Now, the above code will work.

But, I assume you want the greeting attribute to be overwritten? Otherwise you get this

print(child_a.greeting) # Hi I'm a Parent Object!
print(child_a.childgreeting) # Hi I'm a Child Object!

If that is the case, you simply change childgreeting to greeting. Then, from the first example

print(p.greeting) # Hi I'm a Parent Object!
print(child_a.greeting)  # Hi I'm a Child Object!

how do I get them to print as an object?

Not entirely sure what you mean by that, but if you define __str__ to return greeting

class Parent:
    """Parent Object"""

    def __init__(self):
        self.greeting = "Hi I'm a Parent Object!"

    def __str__(self):
        return self.greeting

The example now becomes

print(p) # Hi I'm a Parent Object!
print(child_a)  # Hi I'm a Child Object!
Sign up to request clarification or add additional context in comments.

1 Comment

Yes! This is exactly what I was wondering, brilliantly explained. I'm sorry if my question was a little vague, trying to put it in the best terms I could.
0
class Parent(object):

    def __init__(self):
        self.greeting = "Hi I am a parent"
    def __str__(self):
        return self.greeting


class Child(Parent):

    def __init__(self):
        super(Child, self).__init__()
        self.greeting = "Hi I am Child"

    def __str__(self):
        return self.greeting


p = Parent()
print(p)
c = Child()
print(c)

This method could help you to use 'print()' statement to print out greeting for individual class. But if you want to directly get self.greeting attribute you should use

p = Parent()
print(p.greeting)

Hope I understood you since your questions seems to be not properly explained...

1 Comment

Yes! Thanks for understanding! Sorry it was a little vague!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.