Useful Functions
isinstance()
The isinstance()
(is instance) is a Python built-in function that checks object is an instance of a certain class and return the bool value (True
or False
). This function takes 2 arguments: instance and class:
12print(isinstance(662, int)) print(isinstance(25.3, int))
You can use this function to check your instances:
123456class User: pass user = User() print(isinstance(user, User))
issubclass()
The issubclass()
(is subclass) is a Python built-in function that checks class is a Child of another class:
1234567891011class First: pass class Second(First): pass class Third(Second): pass print(issubclass(Second, First)) print(issubclass(Third, First))
The issubclass()
function works with classes only. If you want to check the instance, use the type()
function inside the issubclass()
:
123456789class A: pass class B(A): pass instance = B() print(issubclass(type(instance), A))
Grazie per i tuoi commenti!
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Mi faccia domande su questo argomento
Riassuma questo capitolo
Mostri esempi dal mondo reale
Awesome!
Completion rate improved to 2.78
Useful Functions
Scorri per mostrare il menu
isinstance()
The isinstance()
(is instance) is a Python built-in function that checks object is an instance of a certain class and return the bool value (True
or False
). This function takes 2 arguments: instance and class:
12print(isinstance(662, int)) print(isinstance(25.3, int))
You can use this function to check your instances:
123456class User: pass user = User() print(isinstance(user, User))
issubclass()
The issubclass()
(is subclass) is a Python built-in function that checks class is a Child of another class:
1234567891011class First: pass class Second(First): pass class Third(Second): pass print(issubclass(Second, First)) print(issubclass(Third, First))
The issubclass()
function works with classes only. If you want to check the instance, use the type()
function inside the issubclass()
:
123456789class A: pass class B(A): pass instance = B() print(issubclass(type(instance), A))
Grazie per i tuoi commenti!