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 to understand Python usage behavior

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "how to understand Python usage behavior". In daily operation, I believe many people have doubts about how to understand Python usage behavior. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to understand Python usage behavior"! Next, please follow the editor to study!

Everything in Python is an object-including numbers. This is important because the numbers-5; 256 are assigned at run time, and accessing them returns a reference to the object, thus allowing you to permanently change the values of these numbers (in the Python instance). Now I don't know how many practical uses it has, but it's really interesting.

Warm up

You need some basic C knowledge and ctypes libraries. First, change the value of a relatively unused number, for example, 31:

> > import ctypes > > def changeNum (oldNum, newNum):... Ctypes.cast (id (oldNum), ctypes.POINTER (ctypes.c_int)) [6] = newNum > > changeNum (31,100) # changes 31 to 100 > 31100

Let's try some basic algorithms:

> 31 + 31200 > > 31 * * 0.510.0 > 31 * 210000

This output makes me feel very uncomfortable, and if you feel the same way, I'm sorry about what's about to happen. Now, let's actually run the glove:

> 31 = = 100True > changeNum (100,200) > 31100 > 100200 > 31 = = 100False > 31 = = 200False > > 31 * 2 = = 200True > 31 * 2 = = 100True

After changing the value, it disappears. You can try to retrieve the original value, but you have deleted it.

> changeNum (100,500 / / 5) > 100200 > changeNum (100,50 * 2) > 100200

Absolute chaos.

If you are not aware of it, the object itself changes globally. This means that any interaction with the number is "undefined". Let's look at how behavior is "defined" in the for loop:

> changeNum (5100) > for i in range (5):. Print (I)... 012... 99

Quite standard; a standard available as an inconsistent digital system. It becomes strange: if I change the value of 5, 5 no longer exists technically. This can lead to very strange interactions in basic operations:

> changeNum (5,20) > 5-7 = = 13 True > 5-7-8 = = 5 True

If you really want to hurt your brain, mess up a few other numbers and do some math:

> changeNum (29,100) > changeNum (5,20) > changeNum (120,200) > 5 + 9100 > 5 + 9 + 5200 > 5 + 9 + 5 + 5220 > (5 + 9) * 52000

You can also make a very confusing infinite while loop:

> > while 5 / / 4 = = 5:... Pass... # Do loop stuff

Crumbling Python

Let's go fishing for big fish now; we've solved other unimportant numbers, but what happens if we change 1?

> changeNum (1,2) > 1 Segmentation fault (core dumped)

It collapsed. This is not surprising, because 1 is a very important number used in a very important calculation. I'm not sure if changing 1 will affect True, but if so, I'll imagine the consequences.

At this point, the study on "how to understand Python usage behavior" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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