On 3/3/23 03:32, Chris Angelico wrote:
> On Fri, 3 Mar 2023 at 20:44, Alan Gauld wrote:
>> On 02/03/2023 20:54, Ian Pilcher wrote:

>>> Seems like an FAQ, and I've found a few things on StackOverflow that
>>> discuss the technical differences in edge cases, but I haven't found
>>> anything that talks about which form is considered to be more Pythonic
>>> in those situations where there's no functional difference.
>>
>> I think avoiding dunder methods is generally considered more Pythonic.

Outside of writing dunder methods, I tend to agree.

>> But in this specific case using isinstance() is almost always
>> the better option.

True. IIRC, the only time I haven't used `isinstance` is in `Enum`, where a particular object has to be exactly a tuple (not a namedtuple, for example) to work correctly.

> Using isinstance is very different from querying the type of an object
> though. They're used for different purposes. And obj.__class__ and
> type(obj) are different too, which is why the OP specifically narrowed
> this down to the situations where you know they're the same.

When writing classes and subclasses, I use `obj.__class__`, `isinstance` otherwise, and rarely `type(obj)` (and then mostly with `tuple`s, as they're special).

~Ethan~
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to