isinstance()

dn PythonList at DancesWithMice.info
Wed Aug 2 18:14:56 EDT 2023


Can you please explain why a multi-part second-argument must be a tuple 
and not any other form of collection-type?


The signature is: isinstance(object, classinfo)
leading to "classinfo" of:

1/ a single class/type, eg int
2/ a tuple of same, eg ( int, str, )
3/ a union type, eg int | str (v3.10+)

A question was asked about this at last night's PUG-meeting. The person 
was using the union option, but from Python v3.8. Sorting that out, he 
replaced the union with a list. No-go! Before correcting to a tuple...

Why does the second argument need to be a tuple, given that any series 
of non-keyword arguments is a tuple; and that such a tuple will still 
need to be delimited by parentheses. In other words, other forms of 
delimiter/collection, eg list and set; being a series of 
elements/members would seem no different.

Yes, the underlying C operation appears to only accept a single argument 
(am not C-soned, mea culpa!).

There is some discussion about hashability, but isn't it coincidental 
rather than constructive? (again, maybe I've not understood...)

Enquiring minds want to know...


Web.Refs:
https://docs.python.org/3/library/functions.html?highlight=isinstance#isinstance
https://docs.python.org/3/c-api/object.html?highlight=isinstance#c.PyObject_IsInstance
https://docs.python.org/3/reference/datamodel.html?highlight=isinstance

-- 
Regards,
=dn


More information about the Python-list mailing list