Created
March 7, 2019 02:54
-
-
Save beam2d/3d1cd3baca30793ff804e7075051d6da to your computer and use it in GitHub Desktop.
pyfinal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import six | |
def final(f): | |
f.__is_final = True | |
return f | |
class enable_final(type): | |
def __new__(cls, name, bases, d): | |
for k in d: | |
for base in bases: | |
f = getattr(base, k, None) # base method | |
if getattr(f, '__is_final', False): | |
raise TypeError('method {} is final'.format(k)) | |
return type.__new__(cls, name, bases, d) | |
class A(six.with_metaclass(enable_final)): | |
@final | |
def foo(self): pass | |
def bar(self): pass | |
class B(A): | |
def bar(self): pass # ok | |
class C(A): | |
def foo(self): pass # ng |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment