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

How does Python get variables by address

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail "Python how to obtain variables through the address", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "Python how to obtain variables through the address" can help you solve your doubts.

Get variables by address

Similar to making a pointer point to the address of a variable in C _ pointer +, the value of the variable can be obtained by dereferencing.

Use the ctypes module to implement similar functions in Python:

Import ctypes value='hello world' # defines a string variable address=id (value) # gets the address of value and assigns it to addressget_value=ctypes.cast (address, ctypes.py_object). Value # reads the variable print (get_value) in the address

Get the output:

Hello world

What is the variable of python? A simple understanding of variables are pointers

In a word:

Everything is an object, and variables are pointers.

Here's a brief introduction to what the pointer is:

Pointer originated from c language, in c language, each piece of memory (or each segment) has a separate variable to identify this block of memory. We can apply for such a variable to point to a piece of memory. Naturally, you can also change the variable to which a piece of memory belongs, and this variable is also called address or pointer.

In the c language, the application variable a

We need to identify the type of variable, so after we finish this application operation, we are equivalent to applying for an int (integer) size memory and an address (& a) of this memory at the same time.

* applying for variables in python does not require a separate indication of the type of application variables, but simply assigns values.

This is the unique method of eliminating the concept of pointers in python-- directly replacing all variables with pointers.

It's kind of like joining in if you can't beat it:

So when you apply for memory space through variables in python, you essentially apply for an address that tells the computer where to start storing a large chunk of memory.

So it's easy to find an interesting phenomenon. There are three types of variables commonly found in python, and their upper limits are almost the same:)

Because compared to the c language, the application variable itself limits the maximum size of the variable (some called memory management), and python opens the door for you (apply for a starting address) you can put as much as you want (in theory)

But there is also an upper limit (blow up your entire piece of memory in order to place an endless loop in a variable)

We can look at the maximum supported integer values through the maxsize function of the sys module:

After reading this, the article "how to obtain variables by Python through address" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself. If you want to know more about related articles, you are welcome to follow the industry information channel.

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