logo

Metoda Python isinstance().

Funkce isinstance() v Pythonu vrátí True, pokud je objekt zadaného typu, a pokud se neshoduje, vrátí False. V tomto článku uvidíme, jak metoda isinstance() funguje v Pythonu

Příklad

  Input:   isinstance([1, 2, 3], list)   Output:   True   Explanation:   The first parameter passed is of list type.   Input:   isinstance(10, str)   Output:   False   Explanation:   The first parameter, 10 is an integer and not a string.>

Syntaxe funkce isinstance() Pythonu

Metoda isinstance() v Krajta má následující syntaxi:



Syntax:

isinstance(obj, class)

Parametry:

  • obj: Objekt, který je třeba zkontrolovat jako součást třídy nebo ne.
  • třída: třída/typ/n-tice třídy nebo typu, proti kterému je třeba objekt kontrolovat.

Vrátí: Je pravda, že pokud objekt patří do dané třídy/typu, pokud je předána jedna třída, nebo jakákoli třída/typ, pokud je předána n-tice třídy/typu, jinak vrátí False.

Chyba typu: pokud je něco jiného než uvedený platný typ třídy.

Jak funguje instance() funkce v Pythonu?

V Pythonu funguje metoda instance() jako operátor porovnání. Vyžaduje dva argumenty, jeden je objekt Pythonu a druhý je typ třídy. Porovná objekt se zadaným typem třídy nebo podtřídy a vrátí booleovskou hodnotu, která je buď True nebo False.

Python3




numbers>=> [>1>,>2>,>3>,>4>,>2>,>5>]> # Check if 'numbers' is an instance of a list> result>=> isinstance>(numbers,>list>)> if> result:> >print>(>'The variable 'numbers' is an instance of a list.'>)> else>:> >print>(>'The variable 'numbers' is not an instance of a list.'>)>

>

mamta kulkarni herec

>

Výstup

The variable 'numbers' is an instance of a list.>

Příklady metody isinstance() v Pythonu

Můžeme poskytnout jeden typ třídy nebo a Pythonská n-tice tříd na metodu instance(). V případě n-tice metoda instance() zkontroluje všechny prvky v n-tici a vrátí True, pokud je objekt instancí některého z prvků n-tice, jinak vrátí False. Podívejme se na několik příkladů metody instance() Pythonu.

Python je instance s Int a List

V tomto příkladu uvidíme, jak metoda isinstance() pracuje s datovým typem Integer as Seznam Pythonu . Zkontrolujeme, zda celé číslo a seznam jsou instancí typu Integer nebo String.

Python3




# initializing native types> test_int>=> 5> test_list>=> [>1>,>2>,>3>]> # testing with isinstance> print>(>'Is test_int integer? : '> +> str>(>isinstance>(test_int,>int>)))> print>(>'Is test_int string? : '> +> str>(>isinstance>(test_int,>str>)))> print>(>'Is test_list integer? : '> +> str>(>isinstance>(test_list,>int>)))> print>(>'Is test_list list? : '> +> str>(>isinstance>(test_list,>list>)))> # testing with tuple> print>(>'Is test_int integer or list or string? : '> >+> str>(>isinstance>(test_int, (>int>,>list>,>str>))))>

>

>

Výstup

Is test_int integer? : True Is test_int string? : False Is test_list integer? : False Is test_list list? : True Is test_int integer or list or string? : True>

Ukázka použití isinstance() s objekty

V tomto příkladu zkontrolujeme třídu objektu v Pythonu, tj. jestli je objekt instancí třídy nebo její odvozené třídy.

Python3




# declaring classes> class> gfg1:> >a>=> 10> # inherited class> class> gfg2(gfg1):> >string>=> 'techcodeview.com'> # initializing objects> obj1>=> gfg1()> obj2>=> gfg2()> # checking instances> print>(>'Is obj1 instance of gfg1? : '> +> str>(>isinstance>(obj1, gfg1)))> print>(>'Is obj2 instance of gfg2? : '> +> str>(>isinstance>(obj2, gfg2)))> print>(>'Is obj1 instance of gfg2? : '> +> str>(>isinstance>(obj1, gfg2)))> # check inheritance case> # return true> print>(>'Is obj2 instance of gfg1? : '> +> str>(>isinstance>(obj2, gfg1)))>

>

>

Výstup

Is obj1 instance of gfg1? : True Is obj2 instance of gfg2? : True Is obj1 instance of gfg2? : False Is obj2 instance of gfg1? : True>

Python isinstance() s řetězcem

V tomto příkladu použijeme funkci isinstance() s a Pythonský řetězec a zkontrolujte třídu objektu v Pythonu.

Python3

formátovač řetězců




test_str>=> 'techcodeview.com'> print> (>'Is test_str string? : '> +> str>(>isinstance>(test_str,>str>)))>

>

>

Výstup

Is test_str string? : True>

Python isinstance() se slovníkem

Metoda Python isinstance() také funguje s a slovník objekt a zkontrolujte třídu objektu v Pythonu.

Python3




test_dict>=> {>'apple'> :>1>,>'Ball'> :>2> }> print> (>'Is test_str dictionary? : '> +> str>(>isinstance>(test_dict,>dict>)))>

>

>

Výstup

Is test_str dictionary? : True>

Python je instancí s metodami třídy

V tomto příkladu používáme metodu isinstance() ke kontrole hodnoty vrácené funkcí třídy se zadaným typem a ke kontrole třídy objektu v Pythonu.

Python3




class> geeks:> >course>=> 'DSA'> > >def> purchase(obj):> >return> obj.course> > > geeks.purchase>=> classmethod>(geeks.purchase)> str>(>isinstance>(geeks.purchase(),>str>))>

>

>

Výstup

síť a internet
True>

Rozdíl mezi metodami isinstance() a type() v Pythonu

Následující tabulka ukazuje rozdíly mezi metodou isinstance() a metodou typ() metoda v Pythonu.

isinstance()

typ()

Syntaxe: isinstance(objekt, třída) Syntaxe: typ (objekt)

Kontroluje, zda je objekt určitého typu třídy

Vrací typ třídy objektu

Může zkontrolovat, zda objekt patří do třídy a jejích podtříd

Nemůže se zabývat dědictvím

Je rychlejší ve srovnání s type() Je pomalejší než isinstance()
Vrací buď True nebo False Vrací typ objektu
Může kontrolovat více tříd najednou To nemůže udělat
Příklad: isinstance(10, (int, str)) Příklad: typ (10)