3 ms·
monkeypatching, instance checking and class-hook hacks aren't things I'd particularly value in Python, they certainly aren't deal-breakers wrt "major wins that
by Chris2048 10y ago
monkeypatching, instance checking and class-hook hacks aren't things I'd particularly value in Python, they certainly aren't deal-breakers wrt "major wins that Python was bringing you".
I've rarely needed to use these things - I'd sooner make use of multiple-inheritance and use mix-ins.
Compared to a language like Java, first the possibility of Mixins exists, as does Duck-typing.
Further, types are not mandatory, or statically checked. Is is the fact that you don't need to do this is Python that is a win. The fact that (barring some extension) you can't is not an advantage. Optional checking, and dropping verbosity (no check by default, not having to declare something unchecked) is the best of worlds.
> things always boil down to mere conventions that rely on trust of the system implementers
This doesn't mean you should try to reduce dependance on this trust. The less checking you do, the more likely a n issue slips through the net. Furthermore, not all infractions are the same, we can forgive small things. Not everyone should be able to modify import, or class-loading mechanisms freely, or without supervision.
> some kind of complicated static type checker that uses argument type annotations
You can make this argument for, say, jumping to Julia. Does a complication in implementing in Python outweigh the complication of an entire new language? You could argue Python 3 is exactly this...
- p4wnc6 10y ago> instance checking and class-hook hacks These are not remotely "hacks" in Python. They are up-front, first class patterns for Python metaclasses. I do agree that monkeypatching is a hack, and is unfortunately relied upon too often in Python.
- Chris2048 10y agoDepends which Python - Py3 is a bit more robust wrt metaclasses. instance checking on a overridden magic method certainly is a hack, I don't know what youy mean by "first class pattern" - Why add the possibility that the instanceof mechanism has been messed with, thereby throwing off alot of reasonable assumptions about the check?
- p4wnc6 10y agoI mean it is supported directly with __instancecheck__ or __subclasshook__ .. explicitly to allow you to create metaclasses with custom implementations of instance checking or subclassing. To call use of this a hack is similar to saying that overriding __getitem__ is a hack. In Python, it's not a hack, it's a normal, common way of doing many things. You'd mess with the isinstance mechanism exactly when doing so made engineering sense. In Python, making assumptions about what isinstance is doing is a very bad idea, would indicate novice Python understanding -- just like picking up some other class (like say, pandas DataFrame) and assuming something "reasonable" about what __getitem__ does would be a bad idea -- __getitem__ is heavily overloaded for various design and performance reasons in Pandas (and in many, many libraries). You can't (and shouldn't want to) pick up any old class in Python and assume things like operator overloads, hashing, instance checking, item getting, serialization, iteration, etc. etc., work under "reasonable assumptions." Either the class authors implemented the trickery in some way that virtually never impacts anyone, or else you need to know the gory implementation details to effectively reason about it. My experience has been that you should always assume the latter until you have strong evidence of the former.