metóda Python isinstance().

Funkcia Python isinstance(). vráti hodnotu True, ak je objekt špecifikovaného typu, a ak sa nezhoduje, vráti hodnotu False. V tomto článku uvidíme, ako metóda isinstance() funguje v Pythone

Prí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. 

Syntax funkcie isinstance() Pythonu

Metóda isinstance() v Python má nasledujúcu syntax:

Syntax:

isinstance(obj, class)

parametre:

  • obj : Objekt, ktorý je potrebné skontrolovať ako súčasť triedy alebo nie.
  • trieda : trieda/typ/nica triedy alebo typu, voči ktorej je potrebné objekt kontrolovať.

Návraty: Pravda, ak objekt patrí do danej triedy/typu, ak je odovzdaná jedna trieda, alebo do ktorejkoľvek triedy/typu, ak je odovzdaná n-tica triedy/typu, inak vráti hodnotu False.

TypeError: ak je niečo iné ako uvedený platný typ triedy.

Ako funguje funkcia instance() v Pythone?

V Pythone metóda instance() funguje ako operátor porovnávania. Vyžaduje dva argumenty, jeden je objekt Pythonu a druhý je typ triedy. Porovná objekt so špecifikovaným typom triedy alebo podtriedy a vráti boolovskú hodnotu, ktorá je buď True alebo 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.'> )>

Výkon

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

Príklady metódy isinstance() v Pythone

Môžeme poskytnúť jeden typ triedy alebo a Pythonská n-tice tried na metódu instance(). V prípade n-tice metóda instance() skontroluje všetky prvky v n-tici a vráti hodnotu True, ak je objekt inštanciou ktoréhokoľvek z prvkov n-tice, inak vráti hodnotu False. Pozrime sa na niekoľko príkladov metódy Python instance().

Python je inštancia s Int a List

V tomto príklade uvidíme, ako metóda isinstance() funguje s dátovým typom Integer as Zoznam Python . Skontrolujeme, či celé číslo a zoznam sú inštanciou typu Integer alebo 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ýkon

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ážka použitia isinstance() s objektmi

V tomto príklade skontrolujeme triedu objektu v Pythone, tj či je objekt inštanciou triedy alebo jej odvodenej triedy.

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ýkon

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 reťazcom

V tomto príklade použijeme funkciu isinstance() s a Reťazec Python a skontrolujte triedu objektu v Pythone.

Python3




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

Výkon

Is test_str string? : True 

Python isinstance() so slovníkom

Metóda Python isinstance() funguje aj s a slovník objekt a skontrolujte triedu objektu v Pythone.

Python3




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

Výkon

Is test_str dictionary? : True 

Python je inštancia s metódami triedy

V tomto príklade používame metódu isinstance() na kontrolu hodnoty vrátenej funkciou triedy so zadaným typom a kontrolu triedy objektu v Pythone.

Python3




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

Výkon

True 

Rozdiel medzi metódami isinstance() a type() v Pythone

Nasledujúca tabuľka ukazuje rozdiely medzi metódou isinstance() a metódou typ() metóda v Pythone.

isinstance()

typ()

Syntax: isinstance(objekt, trieda) Syntax: typ (objekt)

Kontroluje, či je objekt špecifického typu triedy

Vráti typ triedy objektu

Môže skontrolovať, či objekt patrí do triedy a jej podtried

Nemôže riešiť dedičstvo

Je rýchlejší v porovnaní s typom () Je pomalší ako isinstance()
Vráti hodnotu True alebo False Vráti typ objektu
Môže kontrolovať viacero tried naraz Nemôže to urobiť
Príklad: isinstance(10, (int, str)) Príklad: typ (10)