具有明显语法的python多态函数声明
polypie的Python项目详细描述
具有明显语法的python多态函数声明。只是使用 相同的函数名并用 @polymorphic装饰器。
安装
pip install polypie
要求
- Python3.5+
- typeguard(将自动安装)
示例
fromtypingimportAny,Sequencefrompolypieimportpolymorphic,PolypieException@polymorphicdefexample(a:int,b):print('(1)')@polymorphicdefexample(a:str,b:Any):print('(2)')@polymorphicdefexample(a:Sequence[str]):print('(3)')example(100,200)# (1)example('foo',200)# (2)example(['foo'])# (3)example(('bar','baz'))# (3)try:example({'foo':'bar'})exceptPolypieExceptionasexc:print(exc)# Matching signature <...> not foundclassExample:def__init__(self):self.values={}@polymorphicdefvalue(self,name):returnself.values[name]@polymorphicdefvalue(self,name,value):self.values[name]=valueinstance=Example()instance.value('foo',100)instance.value('bar','baz')print(instance.value('foo'))# 100print(instance.value('bar'))# baz
测试
tox [-e ENV] [----cov]