Abstract Base Class property setter absence not preventing Class instantiation

I’m trying to get abstract properties to work, enforcing property getter & setter definitions in downstream classes.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>from abc import ABC, abstractmethod
class BaseABC(ABC):
@property
@abstractmethod
def x(self):
pass
@x.setter
@abstractmethod
def x(self, value):
pass
class MyClass(BaseABC):
def __init__(self, value):
self._x = value
@property
def x(self):
return self._x
# @x.setter
# def x(self, val):
# self._x = val
obj = MyClass(10)
print(obj.x)
obj.x = 20
print(obj.x)
</code>
<code>from abc import ABC, abstractmethod class BaseABC(ABC): @property @abstractmethod def x(self): pass @x.setter @abstractmethod def x(self, value): pass class MyClass(BaseABC): def __init__(self, value): self._x = value @property def x(self): return self._x # @x.setter # def x(self, val): # self._x = val obj = MyClass(10) print(obj.x) obj.x = 20 print(obj.x) </code>
from abc import ABC, abstractmethod


class BaseABC(ABC):
    @property
    @abstractmethod
    def x(self):
        pass

    @x.setter
    @abstractmethod
    def x(self, value):
        pass


class MyClass(BaseABC):
    def __init__(self, value):
        self._x = value
    
    @property
    def x(self):
        return self._x

    # @x.setter
    # def x(self, val):
    #     self._x = val


obj = MyClass(10)
print(obj.x)
obj.x = 20
print(obj.x)

Having read the documentation it seems to indicate the above should trigger a TypeError, when the class is being build, but it only triggers an AttributeError once the attribute is being set.

Why does the absent setter, explicitly defined in BaseABC through an @abstractmethod, not trigger the expected TypeError?
How does one ensure a setter is required in the daughter class?

1

There is no way to enforce what you’re looking for.

Fundamentally, overriding doesn’t happen at a fine enough level of granularity for this. When you override a property, you override the whole property, getter, setter, deleter, and all. Python doesn’t even care if you override it with another property – you could override it with the string "banana".

All the ABC machinery enforces is that subclasses have to override your abstract property with something concrete, for the subclass to be concrete. It doesn’t care whether that something is a read-write property, or a property at all – "banana" would be considered fine.

TL;DR property doesn’t just override the getter; it overrides the setter with None as well.


In MyClass, property creates a brand new property with the given getter and no setter; it doesn’t simply override the getter of the inherited property. The definition of MyClass.x is equivalent to

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>def x_getter(self):
return self._x
x = property(x_getter, None)
</code>
<code>def x_getter(self): return self._x x = property(x_getter, None) </code>
def x_getter(self):
     return self._x

x = property(x_getter, None)

and that None as a setter is “good enough” as far as ABC is concerned with respect to overriding the abstract setter.

To “inherit” the abstract setter as well, use

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class MyClass(BaseABC):
@BaseABC.x.getter
def x(self):
return self._x
</code>
<code>class MyClass(BaseABC): @BaseABC.x.getter def x(self): return self._x </code>
class MyClass(BaseABC):

    @BaseABC.x.getter
    def x(self):
        return self._x

This creates a new property not from scratch, but from the existing BaseABC.x property, using its (abstract) setter but a new getter (just like x.setter before created a new property using the old getter but a new setter).

To make MyClass instantiable, you still need to provide a concrete setter using x.setter.


Unfortunately, nothing forces you to to use BaseABC.x.getter in place of property. ABC only cares that x gets set to something appropriate. This is a general problem with ABC, not properties in particular. There is only so much you can do at the library level; abstract base classes are not a feature of Python itself.

9

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật