Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python - abstract method in normal class

I was reading official python documentation.

In the mentioned link, the second line states that:

Using this decorator requires that the class’s metaclass is ABCMeta or is derived from it.

But, I was successfully able to define the below given class.

from abc import abstractmethod

class A(object):
    def __init__(self):
        self.a = 5
    @abstractmethod
    def f(self):
        return self.a

a = A()
a.f()

So, the code above worked fine. And also, I was able to create a subclass

class B(A):
    def __init__(self):
        super(B, self).__init__() 

b = B()
b.f()

Without overriding the abstract method defined above.

So, basically does this mean that if my base class's metaclass is not ABCMeta(or derived from it), the class does not behave like an abstract class even though I have an abstract method in it?

That means, the documentation needs more clarity?

Or, is this behaviour useful somehow and I'm missing the point.

like image 664
harmands Avatar asked Mar 01 '18 14:03

harmands


People also ask

Can we use abstract method in normal class?

A normal class(non-abstract class) cannot have abstract methods.

Can we use abstract method without abstract class Python?

And yes, you can declare abstract class without defining an abstract method in it. Once you declare a class abstract it indicates that the class is incomplete and, you cannot instantiate it.

How do you call an abstract method in Python?

To define an abstract method we use the @abstractmethod decorator of the abc module. It tells Python that the declared method is abstract and should be overridden in the child classes. We just need to put this decorator over any function we want to make abstract and the abc module takes care of the rest.

Can a class have 2 abstract methods in Python?

Note the abstract base class may have more than one abstract methods. The child class must implement all of them failing which TypeError will be raised.


1 Answers

So, basically does this mean that if my base class's metaclass is not ABCMeta(or derived from it), the class does not behave like an abstract class even though I have an abstract method in it?

Correct.

All abstractmethod does is mark the method with __isabstractmethod__ = True. ABCMeta does all the real work. Here is the code for abstractmethod:

def abstractmethod(funcobj):
    """A decorator indicating abstract methods.
    Requires that the metaclass is ABCMeta or derived from it.  A
    class that has a metaclass derived from ABCMeta cannot be
    instantiated unless all of its abstract methods are overridden.
    The abstract methods can be called using any of the normal
    'super' call mechanisms.
    Usage:
        class C(metaclass=ABCMeta):
            @abstractmethod
            def my_abstract_method(self, ...):
                ...
    """
        funcobj.__isabstractmethod__ = True
        return funcobj
like image 183
Colin Avatar answered Oct 08 '22 15:10

Colin