No error occurs even if not overriding abstract setter and deleter in a child class in Python

37 Views Asked by At

I defined the abstract getter, setter and deleter with @abstractmethod and @property, @name.setter or @name.deleter in Person abstract class as shown below. *I use Python 3.8.5:

from abc import ABC, abstractmethod

class Person(ABC):
    
    @property
    @abstractmethod
    def name(self): # Abstract getter
        pass

    @name.setter
    @abstractmethod
    def name(self, name): # Abstract setter
        pass

    @name.deleter
    @abstractmethod
    def name(self): # Abstract deleter
        pass

Then, I extended Person abstract class with Student class and overrode only the abstract getter in Student class and instantiated Student class as shown below:

class Student(Person): # Extends "Person" class
    
    def __init__(self, name):
        self._name = name
    
    @property
    def name(self): # Overrides only abstract getter
        return self._name

obj = Student("John") # Instantiates "Student" class
print(obj.name)

But, there was no error getting the proper result below even though I didn't override the abstract setter and deleter:

John

Basically, an abstract method needs to be overridden in a child class otherwise there is an error when the child class is instantiated but there was no error in above case.

Actually, when I didn't override the abstract getter in Student class and instantiated Student class as shown below:

class Student(Person): # Extends "Person" class
    
    def __init__(self, name):
        self._name = name

obj = Student("John") # Instantiates "Student" class
print(obj.name)

I got the error below:

TypeError: Can't instantiate abstract class Student with abstract methods name

So, are there anything wrong?

0

There are 0 best solutions below