Python built-in function (--type)

Source: Internet
Author: User

English documents:

class type (object)

class type (name, bases, dict)

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

isinstance()the built-in function is recommended for testing the type of a object, because it takes subclasses into account.

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 CLA SSEs and becomes the __bases__ attribute, and the dict Dictionary is the namespace containing definitions for Class B Ody and is copied to a standard dictionary to become the __dict__ attribute.

Description

1. When a function passes only one parameter, the type of the parameter object is returned. The return value is a type object, usually the same object that is returned by the object. __ class__.

#Defining Type a>>>classA:name='defined in A'#Create a instance of type a>>> A =A ()#a.__class__ Property>>> A.__class__<class '__main__. A'>#type (a) returns a>>>type (a)<class '__main__. A'>#Test Type>>> type (a) = =atrue

2. Although it is possible to detect whether an object is an instance of a type by using the type function, it is more recommended to use the Isinstance function because the Isinstance function takes into account the inheritance relationship between subclasses of the parent class.

# define type B, inherit a class B (A):     = 2# Create an instance of type B b>>> B = B ()# Use the type function to test whether B is type A, and return false>> > type (b) = = Afalse# Use the Isinstance function to test whether B is type A, returns True>>> isinstance ( B,a) True

3. Another way to use the function is to pass in 3 parameters, and the function will use 3 parameters to create a new type. The first parameter, name, is used as the class name for the new type, that is, the __name__ property of the type, the second parameter is a tuple type, its elements are of the class type and will be used as the base class for the newly created type, the __bases__ property of the type, and the third argument dict is a dictionary. Contains the body definition of the newly created class, whose value is copied into the __dict__ property of the type.

#defines type A, which contains attributes Infoa>>>classA (object): Infoa='some thing defined in A'#defines type B, which contains the attribute Infob>>>classB (object): Infob='some thing defined in B'#defines the type C, which contains the attribute Infoc>>>classC (A, B): Infoc='some thing defined in C'#use the type function to create the type D, which contains the attribute Infod>>> D = Type ('D', (A, B), Dict (infod='some thing defined in D'))#types of C, D>>>C<class '__main__. C'>>>>D<class '__main__. D'>#Create instances of type C, type D, respectively>>> C =C ()>>> d =D ()#output The properties of instance C, instance B, respectively>>>(C.INFOA,C.INFOB,C.INFOC) ('some thing defined in A','some thing defined in B','some thing defined in C')>>>(d.infoa,d.infob,d.infod) ('some thing defined in A','some thing defined in B','some thing defined in D')

Python built-in function (--type)

Related Article

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.