Skip to content

Instantly share code, notes, and snippets.

@laalaguer
Last active October 12, 2021 02:43
Show Gist options
  • Save laalaguer/3501deb2bf83fa7b676cec35eafc2d14 to your computer and use it in GitHub Desktop.
Save laalaguer/3501deb2bf83fa7b676cec35eafc2d14 to your computer and use it in GitHub Desktop.
Python Abstract Override can be done with different func params but same name, no error is raised.
import abc
class A(abc.ABC):
@abc.abstractmethod
def foo(self):
print("abstract foo()")
class B(A):
def foo(self, who:str): # Note: function signature is different
print("hello", who)
b = B()
b.foo("bar") # hello bar
@laalaguer
Copy link
Author

laalaguer commented Oct 11, 2021

Also, a class inherit from abc.ABC can be instantiated, provided that it doesn't contain abstract methods.

# Test if abc.ABC class can be instantiated
import abc

class A(abc.ABC):
  def foo(self):
    print("bar")

a = A()
a.foo() # bar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment