LousAjo
LousAjo

Reputation: 21

Python Class in Class

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'

Upvotes: 1

Views: 1650

Answers (2)

ExceptionFinder
ExceptionFinder

Reputation: 117

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()

Upvotes: 3

Jacob Lee
Jacob Lee

Reputation: 4670

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)

Upvotes: -1

Related Questions