Python/Interface: PythonでInterface的な仕組みを実現する

Python/InterfaceはPythonで、JavaやC#におけるいわゆるinterfaceの機能を実現します.

使用例

class SearchEngine:
  app_key=''
  def search(self, q):
    pass
class YahooSearch:
  app_key=1
  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

ダウンロード

関連情報


Yusuke Yanbe