Python built-in Function-type (object)

Source: Internet
Author: User

Parametric forms Manual Literal translation examples expand reading

Parameter Form type (object) type (name, bases, dict) Manual

With one argument, return the type of object. The return value was a type object and generally the same object as returned by object.__class__.

The isinstance () built-in function is recommended for testing the type of object, because it takes to subclasses into Acco Unt.

With three arguments, return a new type object. This is essentially a dynamic form of the class statement. The name string is the class name and becomes the __name__ attribute; The bases tuple itemizes the base classes and becomes the __bases__ attribute; And the Dict dictionary is the namespace containing definitions for class and are copied to a standard dictionary to B Ecome the __dict__ attribute. For example, the following two statements create identical type objects:

>>>
>>> class X:
...     A = 1
...
>>> x = Type (' x ', (object,), Dict (a=1)

Also Type Objects. literal translation

Returns the type of object when a single parameter is a type object, usually the same as a object.__class__ return object.
For test object types, isinstance () built-in functions are recommended because they can take into account subclasses.

Three parameter returns a new type Object, which is essentially the dynamic form of the class declaration. The name string is the class name, becomes the __name__ property, thebases tuple lists the base class, becomes the __bases__ property, thedict Dictionary is the namespace that contains the class body definition, and copies it to the standard dictionary, which becomes __ Dict__ property. For example, the following two declarations create objects of the same type:

>>>
>>> class X:
...     A = 1
...
>>> x = Type (' x ', (object,), Dict (a=1)

See Type Object instance for details

>>> type (object)
<class ' type ' >
>>> type (type)
<class ' type ' >
> >> type (dict)
<class ' type ' >
>>> type (1)
<class ' int ' >
>>> type (int)
<class ' type ' >
>>> type (' 1 ')
<class ' str ' >
>>> type (b ' 1 ')
< Class ' bytes ' >
>>> type (None)
<class ' Nonetype ' >
>>> type (True)
<class ' bool ' >
Expand your reading

Isinstance ()
Type Object

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.