4 ms·
In a way this is an argument for languages where it's normal to have result-types [0] or exceptions, instead of -1 etc. It just feels wrong to have any potentia
by Terr_ 2y ago
In a way this is an argument for languages where it's normal to have result-types [0] or exceptions, instead of -1 etc. It just feels wrong to have any potential collision or confusion between normal results and error results.
[0] https://en.wikipedia.org/wiki/Result_type https://en.wikipedia.org/wiki/Result_type
- kevinventullo 2y agoWe all know that types are not Pythonic. (I’m only mostly joking)
- folkrav 2y agoAckchyually, Python has pretty strong typing, as far as dynamic languages go.
- bhickey 2y agoSure. Everything is a PyObject.
- nas 2y agoEvery PyObject structure has a ob_type pointer.
- ackfoobar 2y agoEvery Object in Java has a getClass method. If we changed all the static type information in Java to the `Object` type, that'd be pretty close to Python's case. GP's post is probably the "unityped" critique of dynamically typed languages by Robert Harper: https://existentialtype.wordpress.com/2011/03/19/dynamic-languages-are-static-languages/ https://existentialtype.wordpress.com/2011/03/19/dynamic-lan...
- bhickey 2y agoThank you for sharing. I hadn't read that critique but I am in wholehearted agreement. Dynamic typing imposes significant cognitive overhead in exchange the privilege of letting you writing incorrect programs.
- Insanity 2y agoIf you use the actual type system and something like mypy, Python is a joy work with. (For my definition of joy, which includes static typing).
- IshKebab 2y agoActually writing Python is reasonably nice in that case. But dealing with Python infrastructure is so awful as to make the whole experience just bad. uv fixes a lot of that, but I think it will be some time before it's used everywhere, and I have zero hope that the Python devs will ever do the right thing and officially replace Pip with uv.
- arccy 2y agoyou also have to trawl through the flags to make sure it actually checks types, e.g. check_untyped_defs
- kstrauser 2y agoNot really, I don’t think. Python code doesn’t ever really use hash() for anything where specific outputs are expected. Even if hash(a)==hash(b), it’s not implied that a==b or a is b.
- TRiG_Ireland 2y agoYes, but having result types would avoid the need for this special casing.
- Ferret7446 2y agoResult types would also make it less efficient.
- tmvphil 2y agoBut -2 seems like just a bad choice here. -1 and -2 are very "common" integers. Seems they could have just picked a quasi-random large integer to reduce the likelihood of collision. I expect hash functions to have collisions, but I expect it to be "unlikely" in a way that scales with the size of the hash.
- kstrauser 2y agoI don't think that matters here, though. This specific hash function is literally used just for putting values in dict buckets. The docs say: > Hash values are integers. They are used to quickly compare dictionary keys during a dictionary lookup. They're not to be used as a crypto digest. The downside here, then is that if -1 and -2 are used as dict keys, they'll end up bucketed together. Apparently that hasn't been enough of an issue over the years to bother changing it. One advantage is that those values are common enough that it might break code that incorrectly expects hash() to have predictable outputs. If it used 0xffffffff as the value, lots of busted tests may never stumble across it.
- itishappy 2y ago> The downside here, then is that if -1 and -2 are used as dict keys, they'll end up bucketed together. Note that they'll only end up bucketed together in the underlying hashmap. The dictionary will still disambiguate the keys. >>> a = -1 >>> b = -2 >>> {a: a, b: b} {-1: -1, -2: -2}
- nayuki 2y agoIn other words, this is a case of https://en.wikipedia.org/wiki/In-band_signaling https://en.wikipedia.org/wiki/In-band_signaling versus out-of-band signaling https://en.wikipedia.org/wiki/Signaling_(telecommunications)#In-band_and_out-of-band_signaling https://en.wikipedia.org/wiki/Signaling_(telecommunications)... .
- kazinator 2y ago1. But, Python has exceptions! The underlying C language doesn't, but Python's run-time has them and can use them in the C code. 2. It may be an argument for ensuring that absolutely everything that is an object can hash: the object hasher must not have error states. Nobody wants the overhead of a simple hash code being wrapped in a result type.