Python built-in function type () and isinstance () Introduction

Source: Internet
Author: User

Python does not support methods or function overloads, so you have to make sure that you call the function or object you want. What exactly is stored in a name? Quite a lot, especially when this is a type of name. It is often useful to confirm the identity of the type object that is received. To achieve this, Python provides a built-in function, type (). Type () returns the types of any Python object object, not limited to the standard type. Let's look at several examples of using the type () built-in function to return multiple object types through the interactive interpreter:

1>>> Type ("')2<type'Str'>3>>>4>>> s ='XYZ'5>>>type (s)6<type'Str'>7>>>8>>> Type (100)9<type'int'>Ten>>> Type (0+0j) One<type'Complex'> A>>>type (0L) -<type'Long'> ->>> Type (0.0) the<type'float'> ->>> ->>>type ([]) -<type'List'> +>>>type (()) -<type'tuple'> +>>>type ({}) A<type'Dict'> at>>>type (type) -<type'type'> ->>> ->>>classFoo:Pass #New-style class - ... ->>> foo =Foo () in>>>classBar (object):Pass #New-style class - ... to>>> bar =Bar () +>>> ->>>type (Foo) the<type'Classobj'> *>>>type (foo) $<type'instance'>Panax Notoginseng>>>type (Bar) -<type'type'> the>>>type (BAR) +<class '__main__. Bar'>

Python2.2 unifies the types and classes, and if you are using an interpreter below Python2.2, you may see different output results.

1>>> Type ("')2<type'string'>3>>>type (0L)4<type'Long int'>5>>>type ({})6<type'Dictionary'>7>>>type (type)8<type'Builtin_function_or_method'>9>>>Ten>>> type (Foo)#assumes Foo created as in above One<type'class'> A>>> Type (foo)#assumes Foo instantiated also -<type'instance'>

Python built-in function type () and isinstance () Introduction

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.