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

Python playful data 4 - dynamic type shared reference garbage collection parameter passing

編輯:Python

introduction :

Refer to some online materials , Thanks for sharing . This article will delve into Python The characteristics of dynamic types ,Python Everything is the object . more Python Advanced Series , Please refer to Python Advanced learning Play data series

Summary :

  1. Dynamic type Vs. Static type
  2. object Variable quote
  3. Shared reference
  4. Garbage collection of objects
  5. From the perspective of dynamic types, we can see the parameter passing of functions

Dynamic type Vs. Static type

Python Is a dynamically typed language : So-called Python Dynamic type , It is to automatically determine the type of object in the process of program operation .

Dynamic type (dynamic typing) yes Python An important core concept .Python The variable of (variable) No declaration required , While in assignment , Variable can be reassigned to any value , These are all related to the concept of dynamic typing .

Static type (static typing) In language , The type of variable must be declared first , That is, the type of variable has been determined at the moment of creation , Later in use , You can only assign this specified type of data to variables . If you force other irrelevant types of data to be assigned to it , Will cause an error .Java Is a dynamically typed language .

Static type assignment :
int x = 5;
Variable x Saved values 5, Not the address .

The base type stores the actual value , Instead of a reference to an object , So when you assign a value to it , It is directly assigned from one place to another , In this case, the assigned value is the data value saved by the variable .

Dynamic type assignment :
x = 5
Variable x Point to object 5 References to .

object Variable quote

stay Python in , Everything is the object . An object is an entity stored in memory . But we don't have direct access to the object . The object name we write in the program , Just a reference to this object (reference). Reference and object separation , It's the core of dynamic types . A reference can point to a new object at any time , This variable is created when it is assigned to a variable for the first time , The values are associated in the subsequent assignment process .

Variables have no type ,Python Types in only exist in objects , Only the value of an object has a type , Variables are generic , It simply refers to a certain type of object at a certain time in the program . Like defining a =1 ,a = ‘a’, The variable was defined at the beginning a For an object that points to an integer , Then the variable points to an object of type string , so , Variables are of variable type .

When we assign values to variables , such as a=5,python Perform three different operations to complete the assignment .

  1. Create an object representative 5
  2. If there are no variables in the program a, Then create him .
  3. Associate variables with objects 5 Connect .

Variable references are implemented as pointers in memory . Once the variable is used , that Python Automatically connect to the object of the variable . say concretely :

  1. Variables are elements of system tables , It points to the address space where the object is stored .
  2. Object is a piece of allocated memory , Addresses can be connected , There is enough space to represent the value of the object
  3. The referenced procedure automatically completes the procedure that the variable points to the object address , Pointer from variable to object .

Shared reference

Multiple variables refer to immutable objects , Share immutable object references :

a = 'Hello'
b = a
print("before a:{}".format(a))
print("before b:{}".format(b))
a = "Hi"
print("after a:{}".format(a))
print("after b:{}".format(b))

Output :

before a:Hello
before b:Hello
after a:Hi
after b:Hello
a = 'Hello', Variable a Point to a string object in memory 'Hello' References to
b = a, Variable b It should point to a variable a The value of the object address pointed to Also point to the object 'Hello' References to .
a = 'Hi', Variable a Point to a string object in memory 'Hi' References to
here , Variable a No longer point to object 'Hello', Change variables a The object pointed to does not affect b Value , Variable b Still point to the object 'Hello'


So for immutable objects ,Python A variable in is always a pointer to a specified object , Instead of changing the label of the memory area , That is to assign a new value to a variable , Instead of replacing the original value of an object , Instead, create a new object for variable reference

Multiple variables refer to variable objects , Shared mutable object references :

a = [1,'hi',3]
b = a
print("before a:{}".format(a))
print("before b:{}".format(b))
a[0] = 10
print("after a:{}".format(a))
print("after b:{}".format(b))

Output :

before a:[1, 'hi', 3]
before b:[1, 'hi', 3]
after a:[10, 'hi', 3]
after b:[10, 'hi', 3]

a = [1,‘hi’,3]
Variable a Point to One list List objects .list A list object is a reference that contains multiple objects ( Each reference points to an object ,a[0] Point to object 1,a[1] Point to object ‘hi’ ,a[2] Point to object 3)
b = a
Variable b And point to this list List objects .
a[0] = 10
It's not about changing variables a The direction of , It's right a[0], That is, part of the table object ( An element ), To operate , So all references to this object are affected .

In contrast , Our previous assignment operations have no effect on the object itself , Just change the reference to

For mutable objects , Variables do not create a new object , Instead, it follows the previous object , Even if the object has been changed . It can be simply understood as , Both objects point to the memory address of a list at the same time , The list maps the memory addresses of the elements inside , Variable sharing doesn't care about list changes , They only care if the memory space of the list changes , therefore , Mutable objects can change themselves when they are referenced , So there is no need to create new objects , So the shared objects will change with the changes of the previous objects .

A list can reference its elements , Change the object itself (in-place change). This object type , They are called variable data objects (mutable object), Dictionaries are also such data types .

And like the previous numbers and strings , You can't change the object itself , You can only change the direction of the reference , It's called immutable data objects (immutable object).

Garbage collection of objects

Each object has two standard headers , One is the type identifier , Used to mark the object type , The other is the reference counter , It is used to determine whether it is a recyclable object . Obviously , stay Python Only objects in the are classified , A variable simply refers to an object , Variables do not have category distinctions , He just refers to a specific object at a specific time .

For the use of reference counters , Then associated to Python Garbage collection mechanism , When a variable name is assigned to a new object , Then the address space occupied by the old objects will be recycled . The space of old objects is automatically put into the memory space pool , Wait for later objects to use .

for example :

a=5
a='Hello'

In the first sentence ,5 Is an integer object stored in memory . Through the assignment , Variable a Point to object 5 References to .

In the second statement , Create objects in memory ‘Hello’, Is a string (string). Variable a Yes ‘Hello’ References to objects . here , object 5 There are no more references to it .Python The object without reference will be destroyed automatically (destruct), Release corresponding memory .

How does the counter work in the garbage collection process ? The counter records the current number of references to the object , If the counter is set to at some time 0, Is not referenced , Then the memory space of this object will be reclaimed .

Garbage collection of objects has great significance , This makes us in the Python You can use any object in and you don't need to consider freeing up space , No C And C++ A lot of basic code in .

For small integers or short strings , The counter is not marked as 0 It's taken back . This sum Python The caching mechanism ,Python Will cache these objects , Instead of building and destroying them frequently . For general objects ,Python Applicable to garbage collection .

Small integers :
because 111 Objects are cached , therefore Variable a, b, c It's pointing to the same object 111

a=111
b=111
c=a
print("a == c:{ }".format(a == c))
print("a == b:{ }".format(a == b))
print("a is c:{ }".format(a is c))
print("a is b:{ }".format(a is b))

Output :

a == c:True
a == b:True
a is c:True
a is b:True

Short string
because ‘111’ Objects are cached , therefore Variable a, b, c It's pointing to the same object ‘111’

a='111'
b='111'
c=a
print("a == c:{ }".format(a == c))
print("a == b:{ }".format(a == b))
print("a is c:{ }".format(a is c))
print("a is b:{ }".format(a is b))

Output :

a == c:True
a == b:True
a is c:True
a is b:True

From the perspective of dynamic types, we can see the parameter passing of functions

Parameters x It's a new quote , Point to a Object referred to . If the parameter is immutable (immutable) The object of ,a and x References are independent of each other . For parameters x The operation of does not affect the reference a. Such a transmission is similar to C Value passing in languages .

def f(x):
x = 100
print("x:{}".format(x))
a = 1
print("before a:{}".format(a))
f(a)
print("after a:{}".format(a))

Output :

before a:1
x:100
after a:1

If it's delivered variable (mutable) The object of , So changing the function parameters , It's possible to change the original object . All references to the original object are affected , Pay attention to this problem when programming .

def f(x):
x[0] = 100
print("x:{}".format(x))
a = [1,2,3]
print("before a:{}".format(a))
f(a)
print("after a:{}".format(a))

Output :

before a:[1, 2, 3]
x:[100, 2, 3]
after a:[100, 2, 3]

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