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

Why python? everything is a variable.

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

Share

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

This article introduces the relevant knowledge of "why python is everything variable". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

One of the challenging concepts in Python is how to create and assign variables. In programming, box analogies are sometimes used. Each box is a variable, and the contents of that box are its values. This isn't a good analogy, and as I'll show later, it can be very problematic, especially when considering Python. Instead, it is best to think of variable names as labels. They can be placed on boxes, but they don't contain anything. Box (an object) holds the value. A sticker is just a label. In addition, multiple stickers can be placed on any box. In short, we changed objects in Python, not their names.

Two useful functions: id() and type()

In the following example, I'll explore how Python interacts with a variety of mutable and immutable objects. To fully examine its behavior, I'll use the id() and type() functions extensively. First, let's look at these two functions so we can understand their outputs. id() takes an object as an argument and returns its id(a number). At the bottom, this is actually the memory address (in CPython implementations) where C stores objects. We can compare IDs of different names to see if they point to the same object, since each object has a unique ID. We'll talk about it later, but for now, a simple example using id():

>>> a = 5 >>> id(a) 10105216 >>> b = 10 >>> id(b) 10105376

Now let's look at type(). Like id(), type() takes the object as an argument, but returns the object's class type instead of its ID. This will come in handy as we start picking different types of objects and their mutability/immutability. Again, let's look at a simple example:

>>> msg = 'hello' >>> type(msg) >>> age = 10 >>> type(msg)

Now that we have a basic understanding of type() and id(), let's use them to start exploring mutable and immutable objects in Python.

mutable object

Mutable objects are objects that can be changed in Python. They are much fewer than immutable objects and include: lists, sets, and dictionaries. They have some interesting behaviors that at first glance seem somewhat confusing. Let's look at some examples of what I mean:

>>> list1 = [1, 2, 3] >>> list2 = list1 >>> id(list1) 140336099032264 >>> id(list2) 140336099032264 >>> list2.append(4) >>> list1 [1, 2, 3, 4]

Here, we create a list named list1 and assign another name (list2) to the list.

Both names refer to the same object, as indicated by their shared ID. We append the list by calling the name list2, and then print the list using list1. It prints out our new list.

Why not? When creating list2, we didn't create another object, we just created another name that points to the same object as list1. Methods do not act on names, they act on objects. So when we type: " list2.append

(4)", we mean: " append 4 to the list object pointed to by list2." What if we want to change list2 but not list1? Well, we have to copy the list first.

>>> list1 = [1, 2, 3] >>> list3 = list1[:] >>> id(list1) 140336099032264 >>> id(list3) 140336098233352 >>> list3.append(4) >>> list1 [1, 2, 3]

We copy the object pointed to by list1 and create a new object from that copy. Now, when we change something in list3, the change is not reflected in list1 because we didn't change the same object. You can prove the same thing by creating two lists that contain the same elements.

>>> list1 = [1, 2, 3] >>> list2 = [1, 2, 3] >>> id(list1) 140397858622984 >>> id(list2) 140397851306184

Each element (in this case an integer) is immutable, but the list itself is mutable. We can add, pop up and modify them as needed and will not make any changes to the other lists. This is not the case for immutable objects.

invariant object

Immutable objects make up most of the objects we will interact with in Python. They include strings, integers, floating-point numbers and tuples and things like that. Let's look at the last example, but this time we'll use two strings instead of two lists:

>>> string1 = "hello" >>> string2 = "hello" >>>id(string1) 140336098225712 >>>id(string2) 140336098225712

See, string1 and string2 have the same ID, which means they are actually the same object. Well, the reason is because strings are immutable. The same is true for other immutable objects:

>>> a = 5 >>> b = 5 >>> id(b) 10105216 >>> id(a) 10105216

Now, if we change the value of a, its id should change.

>>> a = 4 >>> id(a) 10105184

What we need to do is put our name a on a new int object. When we reassign a name to an immutable object, that's exactly what we do.

Mutable and immutable objects in functions

The properties of mutable and immutable objects give them different functions. Although changes to mutable objects are outside the scope of the function, changes to immutable objects do not exist. Let's look at a few examples:

>>> def strFunc(oldString) ... oldString = "goodbye" ... >>> oldString = "hello" >>> strFunc(oldString) >>> print(oldString) hello

We update the string inside the function, but leave it with its old value when we exit. Note that there are no return statements. If we kept returning strings and printing that value, it would be another matter. Now, let's try the same thing for mutable objects:

>>> def listFunc(oldList) ... oldList[0] = 'goodbye' ... >>> oldList = ['hello'] >>> listFunc(oldList) >>> print(ListFunc[0]) goodbye

In this case, we send mutable objects into the function. Then, we update the members of the list, and the update now exists outside the scope of the function.

The content of "Why Python is Everything Variable" is introduced here. Thank you for reading it. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

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