1

Someone can help me to correct this error ? thanks (I'm trying to access a variable in the mother class in the daughter class)

class Animal:
    
    def __init__(self, name):
        self.name = name
    
    class Mammal:
        
        def Presentation(self):
            print(self.name + "is a mammal")
            
dog = Animal("Dog")
dog.Mammal.Presentation()

Traceback (most recent call last): File "", line 11, in TypeError: Presentation() missing 1 required positional argument: 'self1'

4
  • What do you mean by "mother" and "daughter" class? You declared a nested class, but you have neither an instance nor any direct reference to the enclosing class you could use Commented Oct 30, 2020 at 20:59
  • Why do you think Mammal objects will have a self.name attribute? it isn't assigned anywhere? What do you believe nesting a class definition inside another class definition does? It does not create an inheritance relationship. In fact, it doesn't really do anything useful at all, except perhaps keep namespaces seperate Commented Oct 30, 2020 at 21:00
  • "I'm trying to access a variable in the mother class in the daughter class" But you have no reference to any instance of the Animal class, and again, there is no mother/daughter relationship here. Your error occurs because you try to call the method on the class object, not on an instance, but then there is no self, hence the error. Commented Oct 30, 2020 at 21:01
  • A mammal isn't an attribute of an animal; it's a kind of animal. Commented Oct 30, 2020 at 21:07

2 Answers 2

3

Not the actual answer to your question, but I guess you want to do something like this:

class Animal:
    def __init__(self, name):
        self.name = name

    def Presentation(self):
        raise NotImplementedError


class Mammal(Animal):
    def Presentation(self):
        print(self.name + "is a mammal")


dog = Mammal("Dog")
dog.Presentation()

Sign up to request clarification or add additional context in comments.

7 Comments

You don't need to define Mammal.__init__, since it just calls the same method that the expression Mammal.__init__ would resolve to if it weren't defined.
yes thats true, my first unedited answer didn't containt the init function. But I wanted to show how one calls it, in case of changing the parameter "name"
@ExceptionFinder overriding __init__ is not showing anything useful here. The whole point of inheritance is code re-use.
@ExceptionFinder Then at the very least, change the name of the parameter, or do something that warrants overriding the method. As it is now, it's just noise.
@chepner Fine, I removed it :) Although that's not my personal prefrence.
|
-1

The child class would have to inherit the parent self attribute to do that.

class Animal:
    def __init__(self, name):
        self.name = name
    class Mammal:
        def Presentation(inherit):
            print(inherit.name + “is a mammal”)
dog = Animal("Dog")
Animal.Mammal.Presentation(dog)

7 Comments

well, this is a solution, but it isn't inheritance, there is no parent/child class here. This is simply providing an argument to a method. And incorrectly using classes
@AbrarAhmed right, it would have to be Animal.Mammal.Presentation(dog) to not throw an error, regardless, it would still not be the correct way of doing this
I would be sincerely interested in seeing the correct way of doing this for a project I am working on.
@JacobLee correct way of doing what? Using inheritance? Fundamentally, there is no need for nesting classes. That does nothing useful. You inherit from another class by using dosmething like class Child(Parent): ... in the class definition.
Okay, so it just is impractical.
|

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.