5 ms·
> the whole thing seems bolted on, with worse semantics than most modern typed languages One of the biggest sources of ugliness is the "None". The standard wa
by abdullahkhalids 4y ago
> the whole thing seems bolted on, with worse semantics than most modern typed languages
One of the biggest sources of ugliness is the "None". The standard way of declaring variables ahead of time is setting them to None. But then, the type hints just become these ugly unreadable Optional[ActualTypeofVar] everywhere.
- mjr00 4y agoThat's because your type is Optional[TypeOfVar] though; if you try using that variable in a place that expects just TypeOfVar before it's set, you should get an error.
- joshuamorton 4y agoRight, in langs with c-style scoping, this is an issue, but in python if foo: x = a else: x = b Works, so there's no need to predeclare.
- abdullahkhalids 4y agoA very common pattern in python is to declare all instance variables of a class in the __init__ method by setting them to None. This is done for * code readability; all relevant variables are in one place * Python is a prototyping language, and a very common usage pattern is doing dir(object) to determine the names of all variables that could be set to something.
- joshuamorton 4y agoThis is no longer needed with modern python, you can use class Foo: my_int: int To handle this. That said, for the second case, a variable that may be set to something but may not be is optional! That's correct behavior!
- abdullahkhalids 4y agoLet me restate. To prevent ugliness in typing, either * Python should have a way of declaring variables without setting them to None, so we can type hint them to their actual types. Pre-declaration is very important for a prototyping language, and needs to be done in a way that the variable is visible to dir(). * Come up with a cleaner syntax than the wordy Optional[]. Thankfully, they have recently moved on from the ugly Union[a,b] to the more readable a | b. Something more readable could be done for Optional.
- travisjungroth 4y agoa: int b: str | None = None
- thatfunkymunki 4y agobut Optional[foo] is literally a union of foo | None already!
- VygmraMGVl 4y agoYou can declare a type-hint beforehand without setting the value of the variable. See: https://peps.python.org/pep-0526/#global-and-local-variable-annotations https://peps.python.org/pep-0526/#global-and-local-variable-...
- abdullahkhalids 4y agoYes, but then those variables are not visible when we dir(object), which makes it harder for the user to prototype. Certainly, type hinting should not get in the way of the users.
- 9dev 4y agoIm used to just doing x: list[str] if foo: x = [] And if a variable is nullable, I use a union: x: list[str] | None = None That’s much more readable to me!
- pdonis 4y ago> The standard way of declaring variables ahead of time is setting them to None. I'm not sure this method is actually "standard". For many use cases there is an obvious "null" value of the desired type (for example, just set an int variable to 0), so there's no need to use None. For use cases where you can't just initialize the variable to the "null" value of its type (because that value has some other meaning for your program), then the true type of the variable isn't just the type you're thinking (e.g., not just an int), because you need to be able to distinguish the variable having the "null" value from it not being set at all (the None case). And for that kind of use case, Python's type hints are correctly forcing you to declare that variable's type the way that reflects the actual situation.