程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

As a programmer, do you know Python variable reference

編輯:Python

In our programming , Variable is the most basic concept , It's as important as a brick we use to build a building , Is indispensable . therefore , Understanding how variables work is crucial .

Platform on the ninth floor , It starts with soil ; Cuddle wood , Start at the slightest ; A journey , Begins with a single step !

Today, let's talk about Python Those things about variables .

1. Variables are not boxes


Let's look at the following code

1. a = "hello,world"
2. b = a
3. c = [1,2,3]

For us beginners , The assignment of variables is the easiest place to go wrong . What is the most common misunderstanding ?

Define a variable , Just create a variable box in memory , Then put the value of the variable in this box

Let's look at the picture below , This idea is quite wrong . Because of this misunderstanding , So our code may encounter many problems .

What's right ? What do you do when assigning variables ?

2. Strange variables


1. a = "hello,world
2. b = a
3. c = [1,2,3]

So what exactly does the above code do ? before this , Let me tell you a story first .

Last century 90 In time , Our famous Python buffet restaurant has opened . seeing the name of a thing one thinks of its function , This restaurant mainly serves buffet , But what? ? Each customer can only eat one kind of delicious food , And will distribute the delicious food in different rooms , The type of food is not fixed , Provide customers with whatever they need .

On the day , Our little a The students came to the restaurant , Tell the front desk , I want to eat “hello,world”, therefore , A new room was opened at the hotel front desk , Room No :00010, And put our... In it “hello,world”, And he gave the a A pass , This pass can only lead to 00010 Room number . And record hello,world: Number of people to eat :1, When a When you want to eat , Just take your pass 00010 Go to room number to get

Before long , Small b Also came to our hotel , Tell the hotel front desk , I want to talk to you a Eat the same thing . So the hotel front desk , And to b A pass ,b According to the pass, you can also go to 00010 Go to room number to get hello,world. The hotel front desk records again : hello,world: Number of people to eat :2

Then , We are small c The students also came , He followed b Dissimilarity , He has what he wants to eat . He told the hotel front desk : I want to eat [1,2,3]. Customers have new needs , The hotel front desk opened another room , Room No :00020, And also put in it [1,2,3]. Also to c A pass . Then record : [1,2,3]: Number of people to eat :1

We can look down with this story

According to this picture , In our story above :

customer a、b、c: Variable a、b、c\

The hotel : Memory space

front desk :Python Interpreter

room : Memory space divided for objects

Your room number, : The memory address of the object

food : All kinds of objects ( character string 、 list 、 Dictionaries 、 Numbers ...)

The number of people recorded at the front desk : Reference count

Communication Certificate number : The memory address referenced by the variable

In fact, when we assign a value to a variable , Our variable does not store this value . It's bound to a memory address id, When we want to use the value of this variable , Just look for the stored value of this address in memory

And then the story above , Our little a classmate , Tired of eating hello,world, Now I want to eat 123456, So he ran to the hotel front desk and said , I want to eat now 123456 了 , The hotel front desk didn't say a word , A new room has been opened , Room No 00030, There's... In it 123456, And updated a Classmate's pass , At this time, this communication card can only go to 00030 Room number, eat 123456. The front desk continues to record hello,world: Number of people to eat :1、123456: Number of people to eat :1

In the code , We have changed a The value of the variable , What will happen ?

Let's see , change a What happens to the variable ?

a = 123456

Is that going to happen ?

We change a When , It won't change directly a The value stored at the memory address pointed to , Instead, open up a new space to store new values 123456, hold a Change the point to the address of the new space 00030, As shown in the figure below . The right thing to do is :

our b Students like to imitate , She doesn't want to eat now hello,world 了 . So he ran downstairs to the hotel front desk and said : I want to eat [1,2,3]. Watch out! , This time, b What the classmate said is , I want to eat [1,2,3], Not that I want to eat and c Same . Although their food is the same . But our front desk doesn't give it directly to b 00020 The pass for room . It's a new room , Your room number, 00040, Inside [1,2,3], And give b A pass points to 00040 Room number . Colleague records (00040)[1,2,3]: Number of people to eat :1、(00010)hello,world: Number of people to eat :0

b = [1,2,3]

Why? ? In fact, it's easy to understand here , Because we b When assigning a value, an object is created . Just create a new object , Will reopen space .

however , like this

b = c

So there is no new object , It's going to be c Is passed to b, They all point to an object . Here, guys, pay attention , Don't be led astray by my example .

This is the time , Our hotel front desk found 00010 In room No hello,world No one has eaten . At this time, the hotel front desk will take this room back , And put the inside hello,world Throw away the food . This is python Garbage collection mechanism .

here , Again, I thought my classmates d,d Tell the hotel front desk , I want to talk to you c Eat the same , The hotel front desk will give it to d Issued a pass ,d According to the pass, you can go to 00020 Go to room number to get [1,2,3]. The hotel front desk records again : [1,2,3]: Number of people to eat :2

d = c

But our d Students are very picky , He is not satisfied with the existing [1,2,3], He wants to add something , Just tell the front desk to add food : Now 00020 The things in the room become [1,2,3,4]

d.append(4)

here , We found a problem ,c The students did nothing , But the food he can eat from [1,2,3] Turned into [1,2,3,4]

What is the problem ?

Why did we a from “hello,world” become 123456 When , Is to open up a new space . But now d from [1,2,3] become [1,2,3,4], But directly modify it in the original memory space ?

This is it. python Classic interview questions : The variability of objects ? What are mutable objects , What is immutable object ?

3. Mutable objects versus immutable objects


stay python in , Everything is the object , But these objects are also divided into two categories :

The variable object (3 individual ):List( list )、Dictionary( Dictionaries )、Set( aggregate )

Immutable object (3 individual ):Number( Numbers )、String( character string )、Tuple( Tuples )

Python Variable and immutable data types , It mainly depends on whether the value at the memory address pointed to by the variable will change .

3.2 Immutable object


>>> a = 10000
>>> id(a)
139964684838128
>>> a = 30000 # Immutable object , Change the value of a variable , It's actually instantiating a new object 、 Open up new memory space
>>> id(a) # A new memory address is generated , It means that it is no longer the original object 139964684837872
>>>

3.3 The variable object


>>> a = [1,2,3]
>>> b = a
>>> id(a)
139711046464264
>>> id(b)
139711046464264
>>> b.append(4) # The variable object , Allows you to change the value of an object in place
>>> id(b)
139711046464264 # The memory address has not changed , The description is to change the value in the original memory space
>>> id(a)
139711046464264
>>> b
[1, 2, 3, 4]
>>> a
[1, 2, 3, 4]

summary :


The variable object : The value at the memory address pointed to by the variable can be changed

Immutable object : The value at the memory address pointed to by the variable cannot be changed .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved