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'
2 Answers 2
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()
answered Oct 30, 2020 at 21:05
ExceptionFinder
1372 silver badges11 bronze badges
Sign up to request clarification or add additional context in comments.
7 Comments
chepner
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.ExceptionFinder
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"
juanpa.arrivillaga
@ExceptionFinder overriding
__init__ is not showing anything useful here. The whole point of inheritance is code re-use.chepner
@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.
ExceptionFinder
@chepner Fine, I removed it :) Although that's not my personal prefrence.
|
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)
answered Oct 30, 2020 at 21:05
Jacob Lee
4,7402 gold badges20 silver badges40 bronze badges
7 Comments
juanpa.arrivillaga
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
juanpa.arrivillaga
@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 thisJacob Lee
I would be sincerely interested in seeing the correct way of doing this for a project I am working on.
juanpa.arrivillaga
@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.Jacob Lee
Okay, so it just is impractical.
|
lang-py
Mammalobjects will have aself.nameattribute? 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 seperateself, hence the error.