Open averagehat opened 8 years ago
Maybe 'Iterable'? String is also iterable, but you can define new contract to rule out strings. It is not too nice, maybe it a negation in contract would be useful, e.g. 'not str', but it works this way:
from contracts import contract, new_contract
def not_string(x):
return not isinstance(x,str)
new_contract('notstring',not_string)
def iter(n):
for i in range(n):
yield i
@contract(it='Iterable, notstring')
def pr(it):
for x in it:
print(x)
pr(iter(5))
pr([1,2,3])
pr((1,2,3))
pr(range(5))
pr("string should fail")
You can say: seq(str)
, to mean any Sequence (list, tuple, etc.).
Note: you cannot use it with iterators because it would consume the data.
This "genericity" part is important in PyContracts, but unfortunately I didn't have time to do it properly. If you want to contribute, you can start at the definition of "seq".
write now it is possible to do
but not
or
This really stifles polymorphism. I want it to accept a tuple, or a list, etc. Maybe I'm missing something?
NB: (On a related note, I'd like something that would accept generators too, but that has it's own problems).