Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the properties of Python objects?

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "what are the characteristics of Python objects?". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Python is an object-oriented language from the beginning of its design. It has an important concept, that is, everything is an object.

Although Java is also an object-oriented programming language, its pedigree is not as pure as Python. For example, int, one of the eight basic data types of Java, needs to be wrapped as an Integer class object when it is persisted. But in python, everything is an object. Numbers, strings, tuples, lists, dictionaries, functions, methods, classes, modules, and so on are all objects, including your code.

The concept of object

What exactly is an object? Different programming languages define "objects" in different ways. In some languages, it means that all objects must have properties and methods; in others, it means that all objects can be subclassed.

In Python, the definition is loose, some objects have neither properties nor methods, and not all objects can be subclassed. But everything in Python can be interpreted perceptually as: everything in Python can be assigned to a variable or passed to a function as an argument.

All objects in Python have three properties:

Identity: each object has a unique identity, and the identity of any object can be obtained by using the built-in function id (). This value can be simply thought of as the memory address of the object.

> a = 1 > id (a) > 26188904 # identity is represented by such a string of similar numbers

Type: the type of the object determines what type of value the object can hold, what properties and methods it has, what operations it can do, and what rules it follows. You can use the built-in function type () to see the type of object.

There is no one to answer the question? The editor has created a Python learning exchange QQ group: find like-minded partners to help each other, and there are also good video learning tutorials and PDF e-books in the group! > type (a) > type (type) # everything is an object, and type is also a special object type

Value: data represented by the object

> > A1

Identity, Type, and value are assigned when all objects are created. If the object supports update operations, its value is variable, otherwise it is read-only (numbers, strings, tuples, and so on are immutable). These three properties exist as long as the object exists.

Object properties: most Python objects have properties, values, or methods that are accessed using period (.) tokens. The most common properties are functions and methods. Some Python objects also have data properties, such as classes, modules, files, etc.

Creation and reference of objects

> a = 3

To put it simply, the above code does the following:

Created an object to represent the number 3

If the variable a does not exist, create a new variable a

Connect the variable a to the number 3, that is, a becomes a reference to object 3. Internally, the variable is a pointer to the memory space of the object, especially note that the variable is always connected to the object and not to other variables.

Conceptually, an object is a memory space allocated on the heap to represent the values represented by the object; a variable is an element in a system-created table that has a reference to the object; a reference is a pointer from the variable to the object.

Technically, each object has two standard header information, a type identifier to identify the type, and a referenced counter to determine whether the object needs to be reclaimed. There is also a method of optimizing objects, in which Python caches some immutable objects to reuse them instead of creating new objects each time.

There is no one to answer the question? The editor has created a Python learning exchange QQ group: find like-minded partners to help each other, and there are also good video learning tutorials and PDF e-books in the group! > a = 1 > > b = 1 > id (a) 26188904 > id (b) 26188904 # an and b both point to the same object

Shared reference

In Python, variables are all references to an object. When multiple variables refer to the same object, they become shared references.

> a = 1 > b = a > > a = 2 > b1 # because the variable is only a reference to the object, changing a will not lead to the change of b

But it is different for mutable objects such as lists

There is no one to answer the question? The editor has created a Python learning exchange QQ group: find like-minded partners to help each other, and there are also good video learning tutorials and PDF e-books in the group! > a = [1,2,3] > > b = a > > a [0] = 0 > > a [0,2,3] # here does not change the reference of a, but changes an element of the referenced object > b [0,2,3] # because the referenced object has changed, the corresponding value of b has also changed.

Because of this variability of the list, some accidents may occur when the code performs some operations, so it needs to be copied to keep the original list.

> a = [1,2,3] > b = a [:] > > id (a) 140200275166560 > > id (b) 140200275238712 # because b refers to a copy of a referenced object, the two variables point to different memory spaces > a [0] = 0 > > b [1,2,3] # changing elements in a does not cause changes in b

For types that do not have the concept of fragmentation, such as dictionaries and collections, you can use the copy () method in the copy module to copy

> import copy > b = copy.copy (a)

Object equality

The = = operator is used to test whether the values of two referenced objects are equal

Is is used to compare whether two referenced objects are the same object.

There is no one to answer the question? The editor has created a Python learning exchange QQ group: find like-minded partners to help each other, and there are also good video learning tutorials and PDF e-books in the group! > a = [1,2,3] > > b = a > an is bTrue # an and b point to the same object > a = [1,2,3] > > b = [1,2,3] > > an is bFalse # an and b point to different objects

When the Operand is a smaller number or a shorter string, it is different:

> a = 7 > > b = 7 > an is bTrue # an and b point to the same object

This is due to Python's caching mechanism, where small numbers and strings are cached and reused, so an and b point to the same object.

Object collection mechanism

It is mentioned above that the object contains a reference counter, which records the current number of references to the object. Once the counter of the object is 0, that is, there is no reference to the object, the memory space of the object will be reclaimed. This is the object recycling mechanism in Python, and one of the most obvious benefits is that you don't have to think about freeing up memory space when writing code.

You can query the value of an object counter through the getrefcount () function in the sys module

> import sys > If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report