by calibas · 4y ago · 0 comments
Personally, I'd prefer that isinstance() and issubclass() have predictable behavior.
In general metaprogramming is the kind of thing you probably don't and shouldn't reach for first, in fact it's usually more for libraries and tools vs. your production business logic. It can get difficult to reason about and pass the maintenance of code that heavily uses metaprogramming to other people unfamiliar with it.
the other day, this allowed me to refactor a 300L in 50 which are actually readable
I think an example of `Iterable` (sort of like the one in the article) is a very ham-fisted way of getting this sort of check into Python code.
In the end, doesn't the difference just boil down to
class Iterable(ABC):
@classmethod
def __subclasshook__(cls, C):
return hasattr(C, "__iter__")
vs. def is_iterable(t: Type):
return hasattr(t, "__iter__")
Where the first makes it harder to use `Iterable` incorrectly (i.e. supplying a non-type as parameter).I can imagine that a Java or C# programmer would call the first version more "coherent" because it gives the interface `Iterable` a name explicitly.
Sort of like there's not reaaaaally a reason to use Extension Methods in C# (of course there are, but in a lot of simple scenarios there aren't) as opposed to static methods taking a Type as single parameter.
def foo(bar: Iterable):
pass“but it will be so cool!” worked for me :)