Python Type and __bases__

Source: Internet
Author: User

Type literal meaning is a kind that can be understood as a stencil, generating various instances through the mold. The built-in function type () in Python allows you to see the concrete type of the instance.

What kind of type is it? Take a look at the following example.

>>> type (BOOL)
<type ' type ' >

>>> Type (str)<type'type'>>>> Type (int)<type'type'>>>> Type (list)<type'type'>>>> Type (dict)<type'type'>>>> Type (set)<type'type'>>>> Type (type)<type'type'>

As you can see, bool,str,int,list,dict,set are type types, and these are basic types.

Take a look at these types of instances:

>>>type (True)<type'BOOL'>>>>type (False)<type'BOOL'>>>> Type ('1234')<type'Str'>>>> type ([1,2,3,4])<type'List'>>>> type ({1,2,3,4})<type'Set'>>>> type ({1:'a', 2:'b', 3:'C', 4:'D'})<type'Dict'>

There is also a type called Class, which is also a template, and its instance is called object.

class Myobjecta (object): ...      Pass  >>> type (myobjecta)'type'>

Class has an inheritance relationship, what kind of subclass is it? All classes in Python inherit object, and what type is Object?

class Myobjectsuba (myobjecta):      ... Pass  >>> type (myobjectsuba)'type'>>>> type ( Object)'type'>

It appears that as long as it is class, the types are type. And look at the types of objects they instantiate.

>>> my_object_a = myobjecta ()>>> type (my_object_a)<class' myobjecta '>>>> my_object_sub_a = Myobjectsuba ()>>> type (my_object_sub_a) <class'myobjectsuba'>

If an object is an instance of who it is, then it is the type of person.

Take a look at __bases__, __bases__ is also a built-in function to see the inheritance of class. Let's look at the following kinds of inherited relationships.

class MYOBJECTB (object): ...      Pass  class  myobjectc (myobjecta, MYOBJECTB)     : ... Pass  >>> MYOBJECTC. __bases__ (<class'myobjecta';, <class'  myobjectb'>)

MYOBJECTC inherit Myobjecta, MYOBJECTB. You can see that the __bases__ of MYOBJECTC is a tuple that contains myobjecta and MYOBJECTB.

You can try the Python tutor

Myobjecta's __bases__ is an object, verified under:

Myobjecta. __bases__ ('object';,)

What is the __base__ of the object?

>>> object. __bases__ ()

An empty tuple.

What is the __bases__ of type?

>>> type. __bases__ ('object';,)

Object

Finally, we can sort out a picture like this.

Python Type and __bases__

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.