Basic usage of python built-in function isinstance, pythonisinstance

Source: Internet
Author: User

Basic usage of python built-in function isinstance, pythonisinstance
 Syntax: isinstance (object, type)Purpose: To determine whether an object is of a known type.The first parameter (object) is the object, and the second parameter (type) is the type name (int ...) or a list of type names (int, list, float) is a list ).The return value is Boolean (True or flase ).Returns True if the object type is the same as that of parameter 2. If the two parameters are a single tuples, True is returned if the object type is the same as one of the type names in the tuples.Note: If you do not know the type, you can use type to view the type of the character.

Example: a = 10

Print (type ())

Printed result: <class 'int'>

Below are several examples to help us understand:

Example 1:
Abc = 1 # judge whether it is of the int type
Print (isinstance (abc, int ))
--> Return value True
Print (isinstance (abc, (int, str, float )))
--> Return value True
Print (isinstance (abc, (str, float )))
--> Return value False
Example 2:
Abcd = "1" # determine if it is of the str type
Print (isinstance (abcd, int ))
--> Return value False
Print (isinstance (abcd, (int, str, float )))
--> Return value True
Print (isinstance (abcd, (str, float )))
--> Return value True
Example 3:
Abced = [1, 2, 3] # determine if it is a list
Print (isinstance (abced, (list )))
--> Return value True
Abcdef = {"a": 5, "B": 6} # determine whether the dictionary is used
Print (isinstance (abcdef, (dict )))
--> Return value True
Abcdefg = (, 3) # determine whether it is a tuples
Print (isinstance (abcdefg, (tuple )))
--> Return value True
 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.