• Register

Kaj pomeni isinstance?

+11 votes
288 views
asked Feb 24, 2016 by urskamlakar (4,080 points)
retagged Feb 24, 2016 by janko.slavic

1 Answer

+17 votes
 
Best answer

Na spodnji povezavi najdeš več informacij in obrazložitev.

https://docs.python.org/3/library/functions.html#isinstance

isinstance(objectclassinfo)

Return true if the object argument is an instance of the classinfo argument, or of a (direct, indirect or virtual) subclass thereof. If object is not an object of the given type, the function always returns false. If classinfo is a tuple of type objects (or recursively, other such tuples), return true if object is an instance of any of the types. If classinfo is not a type or tuple of types and such tuples, a TypeError exception is raised.

answered Feb 24, 2016 by matejP (2,510 points)
selected Feb 24, 2016 by urskamlakar
Enostaven zgled:

x=5
print(isinstance(x, int))

python vrne: True

Je neke vrste funkcija, ki jo lahko uporabimo tudi v if stavkih in podobno:

x=5
if isinstance(x, int):
    print ('x je celo število')
else:
    print ('x ni celo število')

python vrne: x je celo število
Glejte tudi 4. predavanje na temo objektnega programiranja:
https://github.com/jankoslavic/pypinm
(iščite "instanca")
...