Python मध्ये, बिल्ट-इन फंक्शन प्रकार() आणि 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()
- प्रकार () वापरून प्रकार निर्धार
- isinstance() वापरून प्रकार निर्धार
- प्रकार() आणि isinstance() मधील फरक
ऑब्जेक्टचा प्रकार ठरवण्याऐवजी, ऑब्जेक्टमध्ये योग्य पद्धती आणि गुणधर्म आहेत की नाही हे निर्धारित करण्यासाठी एखादा अपवाद हाताळणी किंवा अंगभूत फंक्शन hasattr() वापरू शकतो.
ऑब्जेक्ट प्रकार मिळवा आणि तपासा:प्रकार()
type(object) हे एक फंक्शन आहे जे वितर्क म्हणून पास केलेल्या ऑब्जेक्टचा प्रकार परत करते. याचा उपयोग ऑब्जेक्टचा प्रकार शोधण्यासाठी केला जाऊ शकतो.
print(type('string'))
# <class 'str'>
print(type(100))
# <class 'int'>
print(type([0, 1, 2]))
# <class 'list'>
type() चे रिटर्न व्हॅल्यू हे str किंवा int सारखे प्रकार ऑब्जेक्ट आहे.
print(type(type('string')))
# <class 'type'>
print(type(str))
# <class 'type'>
ऑब्जेक्ट प्रकाराचे निर्धारण:type(),isinstance()
प्रकार निश्चित करण्यासाठी type() किंवा 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
isinstance() वापरून प्रकार निर्धार
isinstance(ऑब्जेक्ट, क्लास) हे फंक्शन आहे जे पहिल्या वितर्काचे ऑब्जेक्ट दुसऱ्या वितर्काच्या प्रकार किंवा उपवर्गाचे उदाहरण असल्यास खरे मिळवते.
दुसरा युक्तिवाद अनेक प्रकारचा असू शकतो. जर ते कोणत्याही प्रकारचे उदाहरण असेल, तर खरे दिले जाते.
print(isinstance('string', str))
# True
print(isinstance(100, str))
# False
print(isinstance(100, (int, str)))
# True
type() वापरून प्रकार निर्धाराच्या उदाहरणासारखे फंक्शन खालीलप्रमाणे लिहिले जाऊ शकते
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() मधील फरक
type() आणि 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'>
type() वापरून प्रकार निर्धार फक्त जेव्हा प्रकार जुळतात तेव्हाच खरे मिळवते, परंतु isinstance() अगदी सुपरक्लाससाठी खरे मिळवते.
print(type(derive) is Derive)
# True
print(type(derive) is Base)
# False
print(isinstance(derive, Derive))
# True
print(isinstance(derive, Base))
# True
जरी मानक प्रकारांसाठी, उदाहरणार्थ, बुलियन प्रकार bool (खरे, खोटे), काळजी घेणे आवश्यक आहे. bool हा पूर्णांक प्रकाराचा सबक्लास आहे, त्यामुळे isinstance() ज्या int मधून वारसा मिळाला आहे त्या int साठी देखील true परत येतो.
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