Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Useful Functions | Inheritance
In-Depth Python OOP

bookUseful 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:

12
print(isinstance(662, int)) print(isinstance(25.3, int))
copy

You can use this function to check your instances:

123456
class User: pass user = User() print(isinstance(user, User))
copy

issubclass()

The issubclass() (is subclass) is a Python built-in function that checks class is a Child of another class:

1234567891011
class First: pass class Second(First): pass class Third(Second): pass print(issubclass(Second, First)) print(issubclass(Third, First))
copy

The issubclass() function works with classes only. If you want to check the instance, use the type() function inside the issubclass():

123456789
class A: pass class B(A): pass instance = B() print(issubclass(type(instance), A))
copy

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 6

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Awesome!

Completion rate improved to 2.78

bookUseful Functions

Pyyhkäise näyttääksesi valikon

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:

12
print(isinstance(662, int)) print(isinstance(25.3, int))
copy

You can use this function to check your instances:

123456
class User: pass user = User() print(isinstance(user, User))
copy

issubclass()

The issubclass() (is subclass) is a Python built-in function that checks class is a Child of another class:

1234567891011
class First: pass class Second(First): pass class Third(Second): pass print(issubclass(Second, First)) print(issubclass(Third, First))
copy

The issubclass() function works with classes only. If you want to check the instance, use the type() function inside the issubclass():

123456789
class A: pass class B(A): pass instance = B() print(issubclass(type(instance), A))
copy

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 6
some-alt