Skip to content

Instantly share code, notes, and snippets.

@veloutin
Last active November 30, 2016 02:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veloutin/2ec3e5246651f5de78442516d8e24fc1 to your computer and use it in GitHub Desktop.
Save veloutin/2ec3e5246651f5de78442516d8e24fc1 to your computer and use it in GitHub Desktop.
Example of printing a warning for implicit overriding of functions
#!/usr/bin/env python3
import types
class ExplicitOverloadType(type):
def __init__(self, name, bases, dict):
for key, val in dict.items():
if isinstance(val, types.FunctionType):
if getattr(val, "_is_overriden", False):
continue
for base in bases:
if hasattr(base, key):
print("Warning: {} is implicitly overridden".format(key))
type.__init__(self, name, bases, dict)
def override(f):
f._is_overriden = True
return f
class Base(metaclass=ExplicitOverloadType):
def foo(self):
pass
def bar(self):
pass
class Child(Base):
@override
def foo(self):
pass
def bar(self):
pass
if __name__ == '__main__':
c = Child()
@veloutin
Copy link
Author

$ python3 metalearn.py
Warning: bar is implicitly overridden

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