python garbage collection, and user interaction with the operator

 

A garbage collection mechanism to explain (to know)

 

1. What is the garbage collection mechanism

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

2. 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.

3, the principle of garbage collection 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.

3.1 What is the reference count

        Value is the number of variables associated with the variable name: the reference count is

3.1.1, a direct reference to

x = 10

print(id(x))

y = x

z = x

3.1.2, an indirect reference

        The main type of container for

l = ['a', 'b', x]

print(id(l[2])) 

d = { 'mmm': x} # indirect reference

print(id(d['mmm'])

 

ps:

x=10
l = [ 'a', 'b', x] # l = [ 'a' memory addresses, 'b' of the memory address, memory address 10]
Is the value of x address and id (10) output
 
Even when x = 123
print(l[2])
Results and still is the id value of x address (10)
 
To change the value of x, then
x=10
x=[10,]
l=['a','b',10]

3.2 Clear labeling:

        The original circular reference memory leaks solve problems caused by

l 1=[111,]
l2=[222,]

l1.append(l2) # l1=[值111的内存地址,l2列表的内存地址]
l2.append(l1) # l2=[值222的内存地址,l1列表的内存地址]

# print(id(l1[1]))
# print(id(l2))

# print(id(l2[1]))
# print(id(l1))

# print(l2)
# print(l1[1])

del l1   #解除l1的引用
del l2   #解除l2的引用
导致了l1、l2 没有直接引用,只有间接引用,引用计数互为0,变成垃圾
当我们同时删除l1与l2的时候,会清理到栈区中l1与l2的内容

 

这样栈区中不再有l1与l2,于是列表于是列表1与列表2都没有被标记为存活,二者会被清理掉,这样就解决了循环引用带来的内存泄漏问题。

 3.3 分代回收

       基于引用计数的回收机制,每次回收内存,都需要把所有对象的引用计数都遍历一遍,这是非常消耗时间的,于是引入了分代回收来提高回收效率,分代回收采用的是用“空间换时间”的策略。

a、分代

        核心思想:gc会降低多次扫描但是没有回收的变量的扫描频率

 

b、回收

        优点:提升效率

        缺点:后一代扫描频率低于前一代,变量若回收,则会延迟

 

二 用户交互

1、为什么要有用户交互

        用户交互就是人往计算机中input/输入数据,计算机print/输出结果,为了让计算机能够像人一样与用户沟通交流

2、如何用用户交互

        本质是输入(input)输出(output)

2.1 用户输入

在Python3:
        input会将用户输入的所有内容都存成字符串(str)类型
username = input("请输入您的账号:")  # "egon"
print(username, type(username))

age = input("请输入的你的年龄: ")  # age="18"
print(age, type(age))
age=int(age) # int只能将纯数字的字符串转成整型(int)
print(age > 16)
 
在python2中:
        raw_input():用法与python3的input一模一样
        input(): 要求用户必须输入一个明确的数据类型,输入的是什么类型,就存成什么类型

2.2 字符串格式化输出

2.2.1 %s(字符串型)

        a、值按照位置与%s一一对应,少一个不行,多一个也不行
# res="my name is %s my age is %s" %('egon',"18")
# res="my name is %s my age is %s" %("18",'egon')
# res="my name is %s" %"egon"(只有一个变量可以不加括号)
# print(res)
        b、以字典的形式传值,打破位置的限制
# res="我的名字是  %(name)s 我的年龄是  %(age)s" %{ "age":"18", "name":'egon'}
# print(res)
        c、%s   可以接收任意型,%d只能接收整型(int)
# print('my age is %s' %18)
# print('my age is %s' %[1,23])
# print('my age is %s' %{'a':333})
# print('my age is %d%18)            #  %d只能接收int,不能接受‘18’
# print('my age is %d' %“18”)   

2.2.2 str.format:兼容性好(python2.6引进)

res='我的名字是{}我的年龄是{}'.format('egon',18)
print(res)
缺点:略显麻烦

2.2.3 打破位置的限制,按照key=value传值

res="我的名字是 {name} 我的年龄是 {age}".format(age=18,name='egon')
print(res)

2.2.4 f''(python3.5后 #了解)

x=input('your name:')

y=input('your age:')

res=f'我的名字是{x}我的年龄是{y}'

print(res)

三 运算符

1、算术运算符 

 + - *都遵循基本运算规则

/:带小数

//:只保留整数部分,不四舍五入

%:取余数

**:a ** b a的b次方

2、比较运算(结果True/False):>、>=、<、<=、==、!=(不等于)

3、赋值运算符

3.1 =:变量的赋值

3.2 增量赋值:

age=18

age=age+1  <==>age +=1

age = 19

 

age *=3

age /=3

age%=3

age**=3

3.3 链式赋值

x=y=z=10

id(x)=id(y)=id(z)

 

 

 

3.4 交叉赋值

m=10

n=20

temp=m                <==>   m,n=n,m

m=n             

n=temp

3.5 解压赋值

引入*,可以帮助我们取两头的值,和中间的值
取前三个值
x,y,z,*_=salaries=[111,222,333,444,555] # *会将没有对应关系的值存成列表然后赋值给紧跟其后的那个变量名,此处为_
print(x,y,z)
print(_)

3.6 解压字典默认解压出来的是字典的key

x,y,z=dic={'a':1,'b':2,'c':3}
print(x,y,z)

Guess you like

Origin www.cnblogs.com/NevMore/p/12422616.html