పైథాన్లో, ఒక వేరియబుల్ వంటి వస్తువు యొక్క రకాన్ని పొందడానికి మరియు తనిఖీ చేయడానికి మరియు అది నిర్దిష్ట రకానికి చెందినదా అని నిర్ధారించడానికి అంతర్నిర్మిత ఫంక్షన్ల రకం() మరియు isinstance() ఉపయోగించబడతాయి.
- class type(object) — Built-in Functions — Python 3.10.4 Documentation
- isinstance(object, classinfo) — Built-in Functions — Python 3.10.4 Documentation
నమూనా కోడ్తో పాటు క్రింది విషయాలు ఇక్కడ వివరించబడ్డాయి.
- వస్తువు రకాన్ని పొందండి మరియు తనిఖీ చేయండి:
type()
- వస్తువు రకం యొక్క నిర్ణయం:
type()
,isinstance()
- రకం()ని ఉపయోగించి రకం నిర్ధారణ
- instance()ని ఉపయోగించి రకం నిర్ధారణ
- రకం() మరియు అసమానత() మధ్య వ్యత్యాసం
వస్తువు యొక్క రకాన్ని నిర్ణయించడానికి బదులుగా, ఒక వస్తువు సరైన పద్ధతులు మరియు లక్షణాలను కలిగి ఉందో లేదో తెలుసుకోవడానికి మినహాయింపు నిర్వహణ లేదా అంతర్నిర్మిత ఫంక్షన్ hasattr()ని ఉపయోగించవచ్చు.
వస్తువు రకాన్ని పొందండి మరియు తనిఖీ చేయండి:రకం ()
టైప్(ఆబ్జెక్ట్) అనేది ఆర్గ్యుమెంట్గా పాస్ చేసిన ఆబ్జెక్ట్ రకాన్ని అందించే ఫంక్షన్. ఇది వస్తువు యొక్క రకాన్ని కనుగొనడానికి ఉపయోగించవచ్చు.
print(type('string'))
# <class 'str'>
print(type(100))
# <class 'int'>
print(type([0, 1, 2]))
# <class 'list'>
రకం() యొక్క రిటర్న్ విలువ str లేదా int వంటి రకం వస్తువు.
print(type(type('string')))
# <class 'type'>
print(type(str))
# <class 'type'>
వస్తువు రకం యొక్క నిర్ణయం:type(),isinstance()
రకాన్ని నిర్ణయించడానికి రకం() లేదా isinstance()ని ఉపయోగించండి.
రకం()ని ఉపయోగించి రకం నిర్ధారణ
టైప్() యొక్క రిటర్న్ విలువను ఏకపక్ష రకంతో పోల్చడం ద్వారా, ఆబ్జెక్ట్ ఏదైనా రకంగా ఉందో లేదో నిర్ణయించవచ్చు.
print(type('string') is str)
# True
print(type('string') is int)
# False
def is_str(v):
return type(v) is str
print(is_str('string'))
# True
print(is_str(100))
# False
print(is_str([0, 1, 2]))
# False
ఇది అనేక రకాల్లో ఒకటి కాదా అని మీరు గుర్తించాలనుకుంటే, ఇన్ ఆపరేటర్ మరియు టుపుల్ లేదా అనేక రకాల జాబితాను ఉపయోగించండి.
def is_str_or_int(v):
return type(v) in (str, int)
print(is_str_or_int('string'))
# True
print(is_str_or_int(100))
# True
print(is_str_or_int([0, 1, 2]))
# False
ఆర్గ్యుమెంట్ రకాన్ని బట్టి ప్రాసెసింగ్ను మార్చే ఫంక్షన్లను నిర్వచించడం కూడా సాధ్యమే.
def type_condition(v):
if type(v) is str:
print('type is str')
elif type(v) is int:
print('type is int')
else:
print('type is not str or int')
type_condition('string')
# type is str
type_condition(100)
# type is int
type_condition([0, 1, 2])
# type is not str or int
instance()ని ఉపయోగించి రకం నిర్ధారణ
isinstance(object, class) అనేది మొదటి ఆర్గ్యుమెంట్ యొక్క ఆబ్జెక్ట్ రెండవ ఆర్గ్యుమెంట్ యొక్క రకం లేదా సబ్క్లాస్కి ఉదాహరణ అయితే నిజం అని తిరిగి ఇచ్చే ఫంక్షన్.
రెండవ వాదన అనేక రకాలుగా ఉండవచ్చు. ఇది ఏదైనా రకానికి చెందిన ఉదాహరణ అయితే, నిజం తిరిగి ఇవ్వబడుతుంది.
print(isinstance('string', str))
# True
print(isinstance(100, str))
# False
print(isinstance(100, (int, str)))
# True
టైప్()ని ఉపయోగించి టైప్ డిటర్మినేషన్ ఉదాహరణకి సమానమైన ఫంక్షన్ను ఈ క్రింది విధంగా వ్రాయవచ్చు
def is_str(v):
return isinstance(v, str)
print(is_str('string'))
# True
print(is_str(100))
# False
print(is_str([0, 1, 2]))
# False
def is_str_or_int(v):
return isinstance(v, (int, str))
print(is_str_or_int('string'))
# True
print(is_str_or_int(100))
# True
print(is_str_or_int([0, 1, 2]))
# False
def type_condition(v):
if isinstance(v, str):
print('type is str')
elif isinstance(v, int):
print('type is int')
else:
print('type is not str or int')
type_condition('string')
# type is str
type_condition(100)
# type is int
type_condition([0, 1, 2])
# type is not str or int
రకం() మరియు అసమానత() మధ్య వ్యత్యాసం
రకం() మరియు isinstance() మధ్య వ్యత్యాసం ఏమిటంటే, రెండవ ఆర్గ్యుమెంట్గా పేర్కొన్న తరగతిని వారసత్వంగా పొందే సబ్క్లాస్ల ఉదాహరణలకు isinstance() నిజాన్ని అందిస్తుంది.
ఉదాహరణకు, కింది సూపర్క్లాస్ (బేస్ క్లాస్) మరియు సబ్క్లాస్ (ఉత్పన్న తరగతి) నిర్వచించబడ్డాయి
class Base:
pass
class Derive(Base):
pass
base = Base()
print(type(base))
# <class '__main__.Base'>
derive = Derive()
print(type(derive))
# <class '__main__.Derive'>
టైప్()ని ఉపయోగించి టైప్ నిర్ధారణ రకాలు సరిపోలినప్పుడు మాత్రమే నిజమని చూపుతుంది, అయితే సూపర్క్లాస్లకు కూడా isinstance() నిజమని చూపుతుంది.
print(type(derive) is Derive)
# True
print(type(derive) is Base)
# False
print(isinstance(derive, Derive))
# True
print(isinstance(derive, Base))
# True
ప్రామాణిక రకాలకు కూడా, ఉదాహరణకు, బూలియన్ రకం బూల్ (నిజం, తప్పు), జాగ్రత్త తీసుకోవాలి. bool అనేది పూర్ణాంకాల రకానికి చెందిన ఉపవర్గం, కాబట్టి isinstance() అది వారసత్వంగా వచ్చిన పూర్ణాంకానికి కూడా నిజమైనదిగా చూపుతుంది.
print(type(True))
# <class 'bool'>
print(type(True) is bool)
# True
print(type(True) is int)
# False
print(isinstance(True, bool))
# True
print(isinstance(True, int))
# True
మీరు ఖచ్చితమైన రకాన్ని గుర్తించాలనుకుంటే, రకం(); మీరు పరిగణనలోకి తీసుకున్న వారసత్వంతో రకాన్ని గుర్తించాలనుకుంటే, isinstance()ని ఉపయోగించండి.
అంతర్నిర్మిత ఫంక్షన్ issubclass() అనేది ఒక తరగతి మరొక తరగతి యొక్క ఉపవర్గమా కాదా అని నిర్ధారించడానికి కూడా అందించబడుతుంది.
print(issubclass(bool, int))
# True
print(issubclass(bool, float))
# False