google.com, pub-4617457846989927, DIRECT, f08c47fec0942fa0 Learn to enjoy every minute of your life.Only I can change my life.: Python isinstance()

Monday, December 30, 2019

Python isinstance()

In Python isinstance() :

isinstance()

 The Python isinstance() function is used to check whether the object or variable is an instance of the specified class type or data type.

The isinstance() syntax and parameter:

isinstance(object, classinfo)

The isinstance() function checks if the object argument is an instance or subclass of classinfo class argument.

isinstance Parameters:

The isinstance() takes two arguments, and both are mandatory.

object:  The object/instance or variable to check. The object can be any class object or any variable name.

classinfo:  Class info is type name, class name or tuple of types and classes. For example, int, str, list, dict, or any user-created class.

Return Value from isinstance():
If an object or variable is of a specified type, then isinstance() return true otherwise false.


example:
marks = 71
result = isinstance(marks, int)
if result :
  print("Yes! given variable is an instance of type int")
else:
  print("No! given variable is not an instance of type int")

Output:
Yes! given varible is an instance of type int




Using Python isinstance(), you can do the followings:

1.Check the type of a Python variable is of a given type.
2.Verify whether a variable is a number or string.
3.Check if an object is an instance of a specified class.
4.Check whether variable or object is of dict type, list type, set type, tuple type.



isinstance() with Python Class
A isinstance() function test object is of a specified class type. isinstance() is working as a comparison operator because it compares the object with the specified class type.


The isinstance result will be true if the object is an instance of the given class type:

Example:

class Employee:
 
  def __init__(self, name, salary):
    self.name = name
    self.salary = salary

class Person:
 
  def __init__(self, name, sex):
    self.name = name
    self.sex = sex

emp = Employee("Tom", 10000)
per = Person("Cruise", "male")

print("Checking emp object is an instance of Employee")
if isinstance(emp, Employee) :
  print("Yes! given object is an instance of class Employee\n")
else:
  print("No! given object is not an instance of class Employee")

print("Checking per object is an instance of Employee") 
if isinstance(per, Employee) :
  print("Yes! given object is an instance of class Employee")
else:
  print("No! given object is not an instance of class Employee\n")

Output:
Checking emp object is an instance of Employee
Yes! a given object is an instance of class Employee

Checking per object is an instance of Employee
No! given object is not an instance of class Employee


The isinstance function works on the principle of the is-a relationship. The concept of an is-a relationship is based on class inheritance.

No comments:

Post a Comment

शिव भोलेनाथ स्तुति

 जय शिवशंकर, जय गंगाधर, करुणा-कर करतार हरे,   जय कैलाशी, जय अविनाशी, सुखराशि, सुख-सार हरे जय शशि-शेखर, जय डमरू-धर जय-जय प्रेमागार हरे,   जय ...