+3 votes
2 views
in Python by (2.6k points)

How can I check a given object is of a given type? How can I check that a object inherit a given type? 

There's an object o. How can I check that it's a string?

2 Answers

0 votes
by (46.1k points)
edited by

There's no need to check it in Python, but if you still want to check it isinstance(o, str) it will return true if  is a string or inherits from str.

type(o) is str will return true  only if o is str , or false  if o inherits a type str.

Hope this helps. 

0 votes
by (107k points)

To check if o is an instance of str or any subclass of str, use isinstance (this would be the "canonical" way):

if isinstance(o, str):

You can use the following video tutorials to clear all your doubts:-

Related questions

+7 votes
3 answers
+3 votes
2 answers
+1 vote
2 answers
+1 vote
1 answer
+3 votes
2 answers
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...