how to assert that method accepts specific types
Scott David Daniels
Scott.Daniels at Acm.Org
Sat Feb 21 11:49:09 EST 2009
Rhodri James wrote:
> On Sat, 21 Feb 2009 01:12:01 -0000, Darren Dale <dsdale24 at gmail.com> wrote:
>> I would like to assert that a method accepts certain types....
>> from functools import wraps
>> def accepts(*types):
>> def check_accepts(f): ...
>> class Test(object):
>> @accepts(int)
>> def check(self, obj):
>> print obj
>> but now I want Test.check to accept an instance of Test as well....
>
> An icky but relatively clear way to get around this is to gratuitously
> subclass Test:
> class AcceptableTest(object):
> pass
>
> class Test(AcceptableTest):
> @accepts(int, AcceptableTest)
> def check(self, obj):
> print obj
To Increase the ick, but lowering the name pollution
(while making the _source_ read more clearly):
class Test(<Test base classes>):
pass # Just a placeholder for type checking
class Test(Test):
@accepts(int, Test)
def check(self, obj):
print obj
--Scott David Daniels
Scott.Daniels at Acm.Org
More information about the Python-list
mailing list