Python's garbage collection mechanism to interact with the user

Python's garbage collection mechanism to interact with the user

Garbage collection

Garbage collection (referred to as GC) is a mechanism for carrying Python interpreter, designed to recover the value of the variable is not available memory space occupied

Why use garbage collection?

The program is running will apply for large amounts of memory space and memory space for some useless if not cleared up it will lead to the use of depleted memory (memory overflow), causing the program to crash, so memory management is an important and complicated matter, the python interpreter that comes with garbage collection frees the programmer from the complex memory management.

Principle garbage collection mechanism analysis

GC main module of Python used the "reference counting" (reference counting) to track and recover waste. On the basis of the reference count on, can also "tag - Clear" (mark and sweep) to solve the problem cycle container object may be generated by reference, and space for time by "generational recovery" (generation collection) way to further improve the efficiency of garbage collection.

Reference count of
the reference count is: the value of variable frequency and variable names are associated
, such as: age = 18
variable value 18 is associated with a variable name age, called the reference count of 1

** Mark - Clear **
mark / sweep algorithm is the practice when an application available memory space is exhausted, it will stop the entire program, and then does two things, the first is the mark, the second term It is clear

#1、标记
标记的过程其实就是,遍历所有的GC Roots对象(栈区中的所有内容或者线程都可以作为GC Roots对象),然后将所有GC Roots的对象可以直接或间接访问到的对象标记为存活的对象,其余的均为非存活对象,应该被清除。

#2、清除
清除的过程将遍历堆中所有的对象,将没有标记的对象全部清除掉。

Recycling generational
背景
collection mechanism based on reference counting, each reclaim memory, need to count all the objects referenced are traversed again, which is very time consuming, so the introduction of the generational recovered to improve the recovery efficiency, using the recovered generational with "space for time" strategy.
分代
The core idea of generational recycling is: in the case after several scans are not variables are recycled, gc mechanism will think that the variable is a common variable, gc will reduce its frequency sweep

回收
Recycling is still using reference counting as a basis for recovery

User interaction with python

print('*' * 50)
s = input('请输入你的身高:')  # 1. 让程序暂停  # 2. 接收的永远是字符串
print(s, type(s))  # s接收了用户输入的内容
print(int(s) + 1)
print('*' * 50)
# python2和python3中input的区别

# python2和python3多版本共存

# python.exe复制粘贴一份,然后重命名为python2.exe/python3.exe

# 更换python解释器

# file --> settings --> project --> project interpreter
# python2中input和raw_input的区别

#input()
#用户必须得知道python有哪些数据类型才能使用你这个程序,python2的input就被淘汰
print '*'*50
s = input('please enter your name: ')  # 输入什么类型就是什么类型
print s,type(s)
print '*'*50


# raw_input()
print '*' * 50
s = raw_input('please enter your name: ') # 和python3的input一模一样
print s, type(s)
print '*' * 50

Three ways formatted output

The first (mainstream)

# f-string格式化
# f让{}变得有特殊意义,让{}内的普通字符变成了变量名
s1 = 'nick '
s2 = 'handsome'
print(f'{s1}{s2}') # nick handsome

The second (understand)

# %占位符  python3.0 兴许还看得到

s1 = 'nick'
s2 = 'handsome'
print('%s %s'%(s1,s2))

The third (understand)

#format  python3.3  --> 一点毛用都么用

s1 = 'nick'
s2 = 'handsome'
print('{} {}'.format(s1,s2)) # s1-->0 s2-->1

# print('{1} {1}'.format(s1,s2)) # handsome handsome
# 仅作了解

x = 10
print(f'{x:.5f}')  #保留小数个数

s = 'nick'
print(f'{s:*<100}{s:^10}')  # ^表示居中 # <居左  # >居右

unzip

Solution (unlock) compression (data type containers)

lt=[1,2,3,4,5]
# print(lt[1],lt[2],lt[3])

s1,s2,s3,s4,s5=lt
print(s1,s2,s3,s4,s5)
# 下划线_ 代替不要的

# 单个下划线表示这个东西不需要(约定俗成)
lt=[1,2,3,4,5]
s1,_,_,_,_ = lt
print(s1) # 1
# print(_)  # 可以打印,但是不要去打印
# *_  代替好几个下划线_ 方便

# *_  *把后面的元素全部合成放到列表里去了
lt=[1,2,3,4,5]
s1,*_,s5 = lt
print(s1) # 1
# print(_) # [2,3,4]
# 10,20,30 = (10,20,30) = [10,20,30]
x, y, z = 10, 20, 30 #元组 #看成一个列表,用到了解压缩原理
# x, y, z = [10,20,30]
print(x, y, z)

Guess you like

Origin www.cnblogs.com/jzm1201/p/12590470.html
Recommended