Чтобы проверить, является ли переменная кортежем в Python, вы можете использовать следующие методы:
-
Использование функции
type():variable = (1, 2, 3) if type(variable) is tuple: print("The variable is a tuple.") -
Использование функции
isinstance():variable = (1, 2, 3) if isinstance(variable, tuple): print("The variable is a tuple.") -
Проверка атрибута
__class__объекта:variable = (1, 2, 3) if variable.__class__ is tuple: print("The variable is a tuple.") -
Использование функции
type()для сравнения:variable = (1, 2, 3) if type(variable) == tuple: print("The variable is a tuple.") -
Использование модуля
collections.abc:from collections.abc import Iterable variable = (1, 2, 3) if isinstance(variable, Iterable) and not isinstance(variable, str): print("The variable is a tuple.")