那些零碎的知识点

那些零碎的知识点

python - 那些零碎的知识点

一. 字符串格式化

1. "旧式字符串解析(%操作符)"

'Hello, %s' % name
"Hello, Bob" 'Hey %(name)s, there is a 0x%(errno)x error!' % {
"name": name, "errno": errno }
'Hey Bob, there is a 0xbadc0ffee error!'

2. "新式"字符串格式化(str.format)

'Hello, {}'.format(name)
'Hello, Bob' 'Hey {name}, there is a 0x{errno:x} error!'.format( name=name, errno=errno)
'Hey Bob, there is a 0xbadc0ffee error!'

3. 字符串插值/f-Strings(Python 3.6+)

python 3.6新出的,本人用了这个之后, 果断抛弃其他方法, 真的太强大了!!!

name = Bob
f'Hello, {name}!'
'Hello, Bob!' def greet(name, question):
... return f"Hello, {name}! How's it {question}?"
...
greet('Bob', 'going')
"Hello, Bob! How's it going?"

4. 字符串模板法(Python标准库)

templ_string = 'Hey $name, there is a $error error!'

Template(templ_string).substitute(

...     name=name, error=hex(errno))

'Hey Bob, there is a 0xbadc0ffee error!'

二. 小数据池

1. id, is, == =

id 	=> id是内存地址

is 	=> 比较两边的内存是否相等

== 	=> 比较两边的数值是否相等

=	=> 是赋值

2. 小数据池的缓存机制

三. 深浅拷贝

import copy
a = [1,23,66,[6,8]] d = a
b = copy.copy(a)
c = copy.deepcopy(a)
a.append(99)#[1, 23, 66, [6, 8], 99] [1, 23, 66, [6, 8]] [1, 23, 66, [6, 8]] [1, 23, 66, [6, 8], 99]
print(a,b,c,d)
a[3].append(88)#[1, 23, 66, [6, 8, 88], 99] [1, 23, 66, [6, 8, 88]] [1, 23, 66, [6, 8]] [1, 23, 66, [6, 8, 88], 99]
print(a,b,c,d)

垃圾回收机制

哈希

简单说,如果一个对象是可哈希的, 那么生命周期内这个对象不可变,如:int,float,string, tuple.

反之, 不可哈希的对象可变, 像lsit, dict, set.

05-11 14:45