Python built-in function isinstance usage and differences from type

Source: Internet
Author: User

Isinstance is an intrinsic function syntax in Python: Isinstance (object, ClassInfo) If the argument object is an instance of ClassInfo, or object is an instance of a subclass of the ClassInfo class, Returns True. If object is not a given type, the returned result is always false. If ClassInfo does not represent a class (type Object), then it is either a tuple of a class or recursively contains such a tuple of data types. Other sequence types are not allowed. If ClassInfo is not a data type or a tuple of data types, a TypeError exception is thrown.

function: to determine whether an object is a known type.

Its first argument (object) is the second parameter (type) is the type name (int ...). Or a list of type names ((int,list,float) is a list). Its return value is Boolean (True or flase).

Returns true if the type of the object is the same as the type of parameter two. If parameter two is a tuple, returns true if the object type is the same as one of the type names in the tuple.

Example:>>> isinstance (1, int) true>>> isinstance (1.0, float) True >>> a = 4
>>> isinstance (A,int)
True
>>> isinstance (A,STR)
False
>>> Isinstance (A, (str,int,list))
True>>>isinstance (a,dict) Determines whether object A is a dictionary, if true, prints true, such as false, to print false. The difference between type () and Isinstance ():

Common denominator: Both can determine the object type

Isinstance () differs from type ():

    • Type () does not consider a subclass to be a parent class type, regardless of the inheritance relationship.

    • Isinstance () considers the subclass to be a type of parent class, considering the inheritance relationship.

It is recommended to use Isinstance () if you want to determine whether two types are the same.


Different points: For a class class of subclass object type judgment, type is not, and isinstance can.

For example:

class a:passclass b (a): Passisinstance (a (), a) # returns  Truetype (a ()) = = A # returns trueisinstance (B (), a) # returns truetype (b ()) = = a # returns false      
    • In conclusion, it is recommended to use Isinstance to determine the object type.

Isalpha returns a non-zero if it is a letter, otherwise returns to 0

Isalnum returns a non-zero if it is a letter or number, otherwise returns 0

IsDigit if the number (0-9) returns a non-zero; otherwise returns 0

#!/usr/bin/python #-*-coding:utf-8-*- str = "this2009"; # no spaces in characters < Span class= "Hl-code" > print str. Isalnum < Span class= "Hl-code" > str = "this is string EXAMPLE....WOW!!! < Span class= "hl-quotes" > < Span class= "hl-reserved" >print str.isalnum

The result of the above example output is as follows:

TrueFalse

Python built-in function isinstance usage and differences from type

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.