Abstract Method override in Python

If I create an abstract class in python using abc with an abstractmethod as follows and create a concrete class which doesn’t use def to override the abstractmethod but instead attempt to set the attribute in the init function, it continues to throw the error that we can’t instantiate an abstract class. I found out that I can get around this problem by creating a dummy of the abstractmethod. Is this the recommended way?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>from abc import ABC, abstractmethod
class MyAbstract(ABC):
def __init__(self):
pass
@abstractmethod
def hi(self):
pass
class Concrete(MyAbstract):
def __init__(self, selector):
if selector:
self.hi = self.hi_a
else:
self.hi = self.hi_b
def hi(self):
'''
Is it possible to not have to create this function?
'''
pass
def hi_a(self):
print('a')
def hi_b(self):
print('b')
</code>
<code>from abc import ABC, abstractmethod class MyAbstract(ABC): def __init__(self): pass @abstractmethod def hi(self): pass class Concrete(MyAbstract): def __init__(self, selector): if selector: self.hi = self.hi_a else: self.hi = self.hi_b def hi(self): ''' Is it possible to not have to create this function? ''' pass def hi_a(self): print('a') def hi_b(self): print('b') </code>
from abc import ABC, abstractmethod

class MyAbstract(ABC):
  def __init__(self):
    pass

  @abstractmethod
  def hi(self):
    pass

class Concrete(MyAbstract):
  def __init__(self, selector):
     if selector:
       self.hi = self.hi_a
     else:
       self.hi = self.hi_b

  def hi(self):
    '''
    Is it possible to not have to create this function?
    '''
    pass

  def hi_a(self):
     print('a')

  def hi_b(self):
     print('b')

1

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class Concrete(MyAbstract):
hi = None
...
</code>
<code>class Concrete(MyAbstract): hi = None ... </code>
class Concrete(MyAbstract):
    hi = None

    ...

This suffices to allow instantiation. The class simply mustn’t have any attribute decorated with @abstractXXX at time of instantiation. Replacing the inherited hi with any other hi is enough to achieve that.

From documentation:

A class that has a metaclass derived from ABCMeta cannot be
instantiated unless all of its abstract methods and properties are
overridden.

and

Dynamically adding abstract methods to a class, or attempting to
modify the abstraction status of a method or class once it is created,
are only supported using the update_abstractmethods() function.

found out that I can get around this problem by creating a dummy of
the abstractmethod. Is this the recommended way?

Yes, a dummy method or attribute is necessary to tell ABC that the abstract method has been supplied.

Normally, if a method is dynamically added to a class, you can trigger an update to __abstractmethods__ by calling update_abstractmethods(cls). However, your code adds a bound method to the instance. This won’t be detected by the ABC logic. So, nothing short of putting a dummy in the class will suffice since the dynamic update is only done to the instance.

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