𝕨𝕒𝕤𝕒𝕓𝕚
- 4 Posts
- 10 Comments
𝕨𝕒𝕤𝕒𝕓𝕚@feddit.deto
Python@programming.dev•CLI tools hidden in the Python standard library
19·2 years agopython -m http.servercame in handy so many times!
𝕨𝕒𝕤𝕒𝕓𝕚@feddit.deOPto
Python@programming.dev•Generic Typing with `Iterable[str]` or `Collection[str]`: Should you use it?
1·2 years agoYes, that’s a good alternative for
Collection[str]but not so much forIterable[str]as you lose the lazyness of Generators.
𝕨𝕒𝕤𝕒𝕓𝕚@feddit.deOPto
Python@programming.dev•Generic Typing with `Iterable[str]` or `Collection[str]`: Should you use it?
3·2 years agoMaybe something like passing in a list of patterns which should match some data, or a list of files/urls to download would be examples of where I would like to be generic, but taking in a string would be bad.
But the real solution be to convert it to
foo(*args: str). But maybe if you take 2Container[str]as input so you can’t use. But no real world example comes to mind.
𝕨𝕒𝕤𝕒𝕓𝕚@feddit.deOPto
Python@programming.dev•Generic Typing with `Iterable[str]` or `Collection[str]`: Should you use it?
1·2 years agoYes, you’re right. It also a lot of benefits.
𝕨𝕒𝕤𝕒𝕓𝕚@feddit.deOPto
Python@programming.dev•Generic Typing with `Iterable[str]` or `Collection[str]`: Should you use it?
2·2 years agoThis + an assert seems like the way to go. I think that
strshould never have fulfilled these contracts in the first place and should have a.charsproperty that returns a list of one-character-strings. But this change would break existing code, so it is not going to happen.
𝕨𝕒𝕤𝕒𝕓𝕚@feddit.deOPto
Python@programming.dev•Generic Typing with `Iterable[str]` or `Collection[str]`: Should you use it?
2·2 years agostrmatches most of these contracts, though, requiring additional checks if astrwas passed or one of these collections containing strings.
𝕨𝕒𝕤𝕒𝕓𝕚@feddit.deOPto
Python@programming.dev•Generic Typing with `Iterable[str]` or `Collection[str]`: Should you use it?English
3·2 years agoBut what if you actually don’t want
strto be valid?
𝕨𝕒𝕤𝕒𝕓𝕚@feddit.deOPto
Python@programming.dev•Generic Typing with `Iterable[str]` or `Collection[str]`: Should you use it?English
3·2 years agoI know that
IterableandCollectionaren’t the same. My point is, that if you useIterable[str]orCollection[str]as a more flexible alternative tolist[str]you no longer have any type-hinting support protecting against passing in a plain string and you could end up with a subtle bug by unexpectedly looping over['f', 'o', 'o']instead of['foo'].

What’s your hoster?