Home Backend Development Python Tutorial Detailed introduction to Python's object and type

Detailed introduction to Python's object and type

Mar 20, 2017 am 11:25 AM

1. Excerpt from Python Documentation 3.5.2 Partial explanation

Objects are Python's abstraction for data. All data in a Python program is represented by objects or by relations between objects. (In a sense, and in conformance to Von Neumann's model of a “stored program computer,” code is also represented by objects.)

Object is Python’s abstraction of data. All data in a Python program is represented by objects or relationships between objects. (In a sense, and consistent with von Neumann's "stored program computer" model, code is also represented by objects).

Every object has an identity, a type and a value. An object's

identity never changes once it has been created; you may think of it as the object's address in memory. The 'is' operator compares the identity of two objects; the id() function returns an integer representing its identity.

Each object has An identity, a type and a value. The identity of an object does not change once it is created; you can think of it as the address of the object in memory. 'is'

OperatorCompares the identities of two objects; id()FunctionReturns an integer representing their identity.

An object's types determines the operations that the object supports (e.g., “does it have a length?”) and also defines the possible values ​​for objects of that type. The type() function returns an object's type ( which is an object itself). Like its identity, an object's

type is also unchangeable.

The type of an object determines the operations the object supports (for example, "Does it have a length?"), And also defines the possible values ​​for objects of this type. The type() function returns the type of an object (which is the object itself). Like its identity, an object's type is immutable.

2. Pyhtml explanation:

object:

class object
      The most base type
Copy after login

type:

class type(object)
      type(object_or_name, bases, dict)
      type(object) -> the object's type
      type(name, bases, dict) -> a new type
Copy after login

Detailed introduction to Pythons object and type

As can be seen from the above three figures, object object is the most basic type, which is a comprehensive abstract concept of data. Compared with the object object, the type type is a slightly more specific abstract concept. It is said to be specific because it already has the factors to refine the more specific abstract concept from the object object. This is why type(int), type(

float), type(str), type(list), type(tuple), type(set), etc. are all types, which is why The reason why instance(type, object) and instance(object, type) are both True is that the type type is the overall concept of types such as int and float. So, why issubclass(type, object) is True, and issubclass(object, type) is False? From the second picture, that is, from the inheritance relationship, we can see that type is a subclass of object, so the former is True and the latter is False. From the perspective of the overall design of the Python language, there are objects first and then relatively specific types, that is, the design idea of ​​giving priority to the whole over the parts.

If you look at these problems from a more essential perspective, you should find the reasons from Python Documentation-->3. Data Model-->3.1 Objects, values ​​and types [Please refer to Python official standards Library], you can see from the standard library:

  • object is Python’s abstraction of data, and it is the concentrated expression of data by Python programs.

  • Every object has an identity, a type and a value.

  • The type of object determines the operations supported by the object.

  • The values ​​of some objects can be changed. Objects whose values ​​can be changed are called mutable objects; objects whose values ​​cannot be changed after creation are called immutable objects.

Therefore, from the perspective of the overall design system of Python, there are objects first, then identifiers, types and values, followed by operations on objects, etc. This also explains the meaning of Figure 3 The reason for the result.

The above is the detailed content of Detailed introduction to Python's object and type. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

What are regular expressions? What are regular expressions? Mar 20, 2025 pm 06:25 PM

Regular expressions are powerful tools for pattern matching and text manipulation in programming, enhancing efficiency in text processing across various applications.

How does Uvicorn continuously listen for HTTP requests without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

What are some popular Python libraries and their uses? What are some popular Python libraries and their uses? Mar 21, 2025 pm 06:46 PM

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

How to dynamically create an object through a string and call its methods in Python? How to dynamically create an object through a string and call its methods in Python? Apr 01, 2025 pm 11:18 PM

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

See all articles