3 ms·
I mean technically it's a feature you're using, just unintentionally. Kinda like setting a mutable object as a default argument. But I'll admit I got bit by ex
by masklinn 8y ago
I mean technically it's a feature you're using, just unintentionally. Kinda like setting a mutable object as a default argument.
But I'll admit I got bit by exactly this recently, wrote up an "assignment" using a colon instead of an equal sign and it didn't do anything, but didn't fail either.
Somewhat frustratingly, pycharm didn't warn about it either. I should probably check their bug tracker to see if somebody already reported it.
- benatkin 8y agoI don't think so, because 42 is not a type in Python (it could be one day, though - in TypeScript, 42 can be used as a type, basically an enum with only one value). I think (naively) that it should throw an error.
- larkeith 8y agoI'm not sure how feasible throwing an error is - Type hints in Python can be arbitrary classes or instances thereof, in order to type hint for stuff like nested data structures (`Dict[str: int]`). You could disallow instances of specific built-in classes, but that seems... clunky, especially as some can be used in valid code (strings are used as forward references).
- masklinn 8y agoAnnotations were specifically designed as general-purpose, not specifically type annotations. So the ability to put arbitrary expressions in annotations was very much intended. In fact, the type hints of the typing module are not actually types.
- benatkin 8y agoAh, I see, good to know. What I wanted when I found the PEP a few days ago were docstrings for instance variables, which isn't one of the things that this provides, so I stopped reading. I ended up using properties but it's a bit verbose. It's great that typings are in a module so it can be extended or replaced!