Interface on Python
ASPN Python Cookbook The End of Inheritance Automatic Run-time Interface Building for Aggregated Objectsの主張も分かりますが、他の言語で慣れた方法で使いたいということもあり、作ってみました。
http://yanbe.org/python/interface/interface.py
使い方
こんな感じで使えます。
from interface import interface class SearchEngine: app_key='' def search(self, q): pass class YahooSearch: def __init__(self): interface(YahooSearch, SearchEngine) class GoogleSearch: app_key='' def __init__(self): # do not check method arguments interface(GoogleSearch, SearchEngine, check_method_args=False) def search(self, q, start): pass g = GoogleSearch() #ok y = YahooSearch() #ng
実行結果
AssertionError: Method __main__.YahooSearch.search(self, q) is required by __main__.SearchEngine Variable __main__.YahooSearch.app_key <type 'str'> is required by __main__.SearchEngine