內(nèi)存管理和拷貝
review
1.內(nèi)置類(lèi)屬性: name癌椿、doc、class菱阵、dict踢俄、module、bases
2.私有化
3.屬性的getter和setter
4.繼承
class 類(lèi)名(父類(lèi)1,父類(lèi)2):
pass
class Person:
def __init__(self, name, age, gender='男'):
# name = '小明', age = 18, gender='男'
self.name = name
self.age = age
self.gender = gender
class Student(Person):
def __init__(self, name, age, study_id, gender='男'):
# name='小明'送粱, age=18, study_id='001', gender='男'
super(Student, self).__init__(name, age, gender)
self.study_id = study_id
stu = Student('小明', 18, '001')
多繼承
1.多繼承
python中的類(lèi)支持多繼承(讓一個(gè)類(lèi)同時(shí)繼承多個(gè)類(lèi));
多繼承的時(shí)候褪贵,子類(lèi)只能繼承第一個(gè)父類(lèi)所有的屬性和方法,后面的父類(lèi)中只有字段和方法可以被繼承
class Animal(object):
num = 100
def __init__(self):
self.age = 0
self.gender = '雌'
@classmethod
def func1(cls):
print('動(dòng)物類(lèi)的類(lèi)方法')
def func2(self):
print('動(dòng)物類(lèi)中的對(duì)象方法')
class Fly(object):
name = '飛行器'
def __init__(self):
self.height = 100
self.time = 5
self.speed = 100
def func2(self):
print('飛行的對(duì)象方法')
class Bird(Animal, Fly):
pass
bird1 = Bird()
# 字段都能繼承
print(Bird.num, Bird.name)
Bird.func1()
bird1.func2()
# print(bird1.age, bird1.gender)
# print(bird1.speed, bird1.height, bird1.time)
運(yùn)算符重載
import copy
1.運(yùn)算符
python中所有的類(lèi)型都是類(lèi)抗俄,所以所有的數(shù)據(jù)都是對(duì)象;
python中使用任意的運(yùn)算符都是在調(diào)用相應(yīng)類(lèi)中的相應(yīng)方法脆丁,每一個(gè)運(yùn)算符對(duì)應(yīng)什么方法是固定的,
某種數(shù)據(jù)是否支持某個(gè)運(yùn)算符操作就看這個(gè)數(shù)據(jù)類(lèi)型中是否實(shí)現(xiàn)了對(duì)應(yīng)的方法
2.運(yùn)算符重載指的是在不同的類(lèi)中實(shí)現(xiàn)同樣的運(yùn)算符對(duì)應(yīng)的函數(shù)
類(lèi)的對(duì)象默認(rèn)情況下只支持: ==, !=
10 * 20
'abc'+'123'
[1, 2] + [2, 3, 4]
# {'a': 10} + {'b': 20}
class Student:
def __init__(self, name, age, score=0):
self.name = name
self.age = age
self.score = score
def __repr__(self):
return '<%s, id:%s>' % (str(self.__dict__)[1:-1], hex(id(self)))
# a+b -> a.__add__(b)
# self -> 當(dāng)前類(lèi)的對(duì)象动雹,也是+前面的那個(gè)數(shù)據(jù)
# other -> +后面的那個(gè)數(shù)據(jù), 類(lèi)型根據(jù)運(yùn)算規(guī)則的設(shè)計(jì)可以是任何類(lèi)型的數(shù)據(jù)
def __add__(self, other):
# return self.age + other.age
return self.score + other.score
# return Student(self.name+other.name, self.age+other.age, self.score + other.score)
# return self.score + other
# a*b -> a.__mul__(b)
def __mul__(self, other):
list = []
for _ in range(other):
list.append(copy.copy(self))
return list
# a<b -> a.__lt__(b)
# 注意: <和>符號(hào)只需要重載其中一個(gè)就可以
def __lt__(self, other):
return self.score < other.score
stu1 = Student('小明', 19, 90)
stu2 = Student('小花', 20, 78)
# stu1.__add__(stu2)
print(stu1 + stu2)
# stu1.__add__(100)
# print(stu1 + 100)
print(stu1 * 3)
students = [stu1, stu2, Student('小紅', 12, 100)]
students.sort()
print(students)
print(stu1 < stu2)
print(stu1 > stu2)
淺拷貝和深拷貝
from copy import copy, deepcopy
class Dog:
def __init__(self, name, color):
self.name = name
self.color = color
def __repr__(self):
return '<%s,id: %s>' % (str(self.__dict__)[1:-1], hex(id(self)))
class Person:
def __init__(self, name, age, dog=None):
self.name = name
self.age = age
self.dog = dog
def __repr__(self):
return '<%s,id: %s>' % (str(self.__dict__)[1:-1], hex(id(self)))
p1 = Person('小明', 18, Dog('大黃', '黃色'))
1.直接賦值
將變量中的地址直接賦給新的變量槽卫;賦值后兩個(gè)變量的地址相同
p2 = p1
print(id(p1), id(p2)) # 4537270848 4537270848
p1.name = '小花'
print(p1.name, p2.name) # 小花 小花
p2.dog.color = '綠色'
print(p1.dog.color, p2.dog.color) # 綠色 綠色
2.拷貝
不管是淺拷貝還是深拷貝都會(huì)對(duì)原數(shù)據(jù)進(jìn)行賦值產(chǎn)生新的地址
list1 = [1, 2, 3]
list2 = copy(list1)
list3 = deepcopy(list1)
print(id(list1), id(list2), id(list3))
list1.append(100)
print(list2, list3)
p3 = copy(p1)
p4 = deepcopy(p1)
print(id(p1), id(p3), id(p4))
p1.name = '小紅'
print(p3.name, p4.name)
3.淺拷貝
字符串、列表和元祖的切片;對(duì)象.copy(); copy模塊中的copy方法都是淺拷貝
淺拷貝指拷貝當(dāng)前對(duì)象胰蝠,不會(huì)對(duì)子對(duì)象進(jìn)行拷貝
print('==========淺拷貝=========')
p3 = copy(p1)
print(id(p1), id(p3)) # 4401914600 4401916336
print(id(p1.dog), id(p3.dog)) # 4567065152 4567065152
p1.name = 'Tom'
print(p1.name, p3.name) # Tom 小紅
p1.dog.color = '紅色'
print(p1.dog.color, p3.dog.color) # 紅色 紅色
4.深拷貝
copy模塊中的deepcopy方法是深拷貝
print('==========深拷貝=========')
p4 = deepcopy(p1)
print(id(p1), id(p4)) # 4486472592 4486474384
print(id(p1.dog), id(p4.dog)) # 4485063960 4486474552
p1.name = 'Bob'
print(p1.name, p4.name) # Bob Tom
p1.dog.color = '橙色'
print(p1.dog.color, p4.dog.color) # 橙色 紅色
練習(xí):
a = ['color', 'height', 'background']
b = [a, 'aaa', 'bbb']
c1 = b
c2 = copy(b)
c3 = deepcopy(b)
a[-1] = ['BG']
b.append('ccc')
# b = [['color', 'height', 'background'], 'aaa', 'bbb']
# 問(wèn)題: print(c1), print(c2), print(c3)的結(jié)果分別是
# c1 = [['color', 'height', ['BG']], 'aaa', 'bbb', 'ccc']
# c2 = [['color', 'height', ['BG']], 'aaa', 'bbb']
# c3 = [['color', 'height', 'background'], 'aaa', 'bbb']
print(c1)
print(c2)
print(c3)
枚舉
from enum import Enum, unique
枚舉值的特點(diǎn):
1.可以通過(guò)有意義的屬性名直接顯示數(shù)據(jù)
2.每個(gè)數(shù)據(jù)的值不能修改
3.可以做到不同的數(shù)據(jù)的值唯一
@unique
class PokerNum(Enum):
J = 11
Q = 12
K = 13
A = 1
class Color:
RED = (255, 0, 0)
print(PokerNum.J)
print(PokerNum.K.value, PokerNum.J.value > PokerNum.Q.value)
內(nèi)存管理
from copy import copy, deepcopy
from sys import getrefcount
1.內(nèi)存的開(kāi)辟
內(nèi)存區(qū)間分為棧區(qū)間和堆區(qū)間歼培;棧區(qū)間的內(nèi)存自動(dòng)開(kāi)辟自動(dòng)釋放,堆區(qū)間的內(nèi)存需要程序員手動(dòng)開(kāi)辟茸塞,手動(dòng)釋放躲庄;
但是python已經(jīng)將堆區(qū)間內(nèi)存的開(kāi)辟和釋放自動(dòng)化
a.當(dāng)每次給變量賦值的時(shí)候,系統(tǒng)會(huì)先在堆區(qū)間中開(kāi)辟空間將數(shù)據(jù)存起來(lái)钾虐,然后再將數(shù)據(jù)在堆中的地址存到變量中噪窘,變量存在棧區(qū)間;
b.數(shù)字和字符串?dāng)?shù)據(jù)在開(kāi)辟空間的時(shí)候,會(huì)先檢測(cè)內(nèi)存中之前是否已經(jīng)有這個(gè)數(shù)據(jù)效扫,
如果有就直接使用之前的數(shù)據(jù)倔监,沒(méi)有才開(kāi)辟新的空間保存數(shù)據(jù)
a = [1, 2, 3, [1, 2]] # [1, 2, 0xccc]
b = [1, 2, 3, [1, 2]] # [1, 2, 0xccc]
print(id(a), id(b)) # 4484287176 4484285768
print(id(a[3]), id(b[3]))
print(id(a[0]), id(b[0]), id(a[3][0]), id(b[3][0])) # 4449482688 4449482688 4449482688 4449482688
a1 = 100
b1 = 100
print(id(a1), id(b1)) # 4480746528 4480746528
a2 = 'hello'
b2 = 'hello'
print(id(a2), id(b2)) # 4492420184 4492420184
a3 = {'a': 10}
b3 = {'a': 10}
print(a3 == b3) # True
print(a3 is b3) # False
a4 = 200
b4 = deepcopy(a4)
print(id(a4), id(b4)) # 4367007904 4367007904
a5 = '安徽省打發(fā)卡機(jī)阿哈薩克阿按時(shí)發(fā)阿薩德回復(fù)杰卡斯發(fā)是打飛機(jī)啊山東礦機(jī)返回撒嬌快遞費(fèi)哈薩克京東方撒地方'
b5 = '安徽省打發(fā)卡機(jī)阿哈薩克阿按時(shí)發(fā)阿薩德回復(fù)杰卡斯發(fā)是打飛機(jī)啊山東礦機(jī)返回撒嬌快遞費(fèi)哈薩克京東方撒地方'
print(id(a5), id(b5)) # 4338364064 4338364064
2.內(nèi)存的釋放
棧區(qū)間:全局棧區(qū)間在程序結(jié)束后銷(xiāo)毀,函數(shù)棧區(qū)間在函數(shù)調(diào)用結(jié)束后銷(xiāo)毀(自動(dòng))
堆區(qū)間: 看一個(gè)對(duì)象是否銷(xiāo)毀菌仁,就看這個(gè)對(duì)象的引用計(jì)數(shù)是否為0浩习;如果一個(gè)對(duì)象的引用為0,這個(gè)對(duì)象就會(huì)銷(xiāo)毀;
(垃圾回收機(jī)制)
注意: python中針對(duì)對(duì)象的循環(huán)引用已經(jīng)做了處理济丘,程序員不需要寫(xiě)額外的代碼來(lái)解決循環(huán)引用問(wèn)題
a6 = {'name': '小明', 'age': 18}
b6 = a6
print(getrefcount(a6))
b6 = 100
print(getrefcount(a6))
del a6
print(getrefcount(b6))
class Person:
def __init__(self, name):
self.name = name
self.dog = None
class Dog:
def __init__(self, name):
self.name = name
self.owner = None
dog1 = Dog('大黃')
p1 = Person('小明')
p1.dog = dog1
dog1.owner = p1
del dog1
del p1
撲克洗牌發(fā)牌
from enum import Enum,unique
from random import shuffle
# 1.寫(xiě)一個(gè)撲克類(lèi)谱秽, 要求擁有發(fā)牌和洗牌的功能(具體的屬性和其他功能自己根據(jù)實(shí)際情況發(fā)揮)
@unique
class PokerNum(Enum):
J = 11, 'J', 11
Q = 12, 'Q', 12
K = 13, 'K', 13
A = 1, 'A', 14
TWO = 2, '2', 15
BigJoker = 15, '大王', 17
SmallJoker = 14, '小王', 16
@unique
class PokerColor(Enum):
Club = '?'
Diamond = '?'
Heart = '?'
Spade = '?'
Space = ''
class Poker:
def __init__(self, num: PokerNum, color: PokerColor):
self.num = num
self.color = color
def __repr__(self):
if isinstance(self.num, PokerNum):
return str(self.color.value) + str(self.num.value[1])
return str(self.color.value)+str(self.num)
def __gt__(self, other):
if isinstance(self.num, PokerNum):
a = self.num.value[2]
else:
a = self.num
if isinstance(other.num, PokerNum):
b = other.num.value[2]
else:
b = other.num
return a > b
class Game:
# 斗地主
def __init__(self):
pokers = []
colors = [PokerColor.Club, PokerColor.Diamond, PokerColor.Heart, PokerColor.Spade]
nums = [PokerNum.A, PokerNum.TWO, 3, 4, 5, 6, 7, 8, 9, 10, PokerNum.J, PokerNum.Q, PokerNum.K]
for num in nums:
for color in colors:
p = Poker(num, color)
pokers.append(p)
pokers.extend([Poker(PokerNum.SmallJoker, PokerColor.Space), Poker(PokerNum.BigJoker, PokerColor.Space)])
self.pokers = pokers
self.pokers_iter = iter(self.pokers)
# 洗牌
def shuffling(self):
shuffle(self.pokers)
self.pokers_iter = iter(self.pokers)
# 斗地主發(fā)牌方式
def deal1(self):
ps1 = []
ps2 = []
ps3 = []
for _ in range(17):
ps1.append(next(self.pokers_iter))
ps2.append(next(self.pokers_iter))
ps3.append(next(self.pokers_iter))
ps1.sort(reverse=True)
ps2.sort(reverse=True)
ps3.sort(reverse=True)
return ps1, ps2, ps3, list(self.pokers_iter)
game1 = Game()
game1.shuffling()
p1, p2, p3, di = game1.deal1()
print(p1)
print(p2)
print(p3)
print('底牌:', di)
歌詞解釋器
# (嘗試)寫(xiě)一個(gè)類(lèi),其功能是:1.解析指定的歌詞文件的內(nèi)容 2.按時(shí)間顯示歌詞
# 提示:歌詞文件的內(nèi)容一般是按下面的格式進(jìn)行存儲(chǔ)的摹迷。
# 歌詞前面對(duì)應(yīng)的是時(shí)間弯院,在對(duì)應(yīng)的時(shí)間點(diǎn)可以顯示對(duì)應(yīng)的歌詞
class Lyric:
def __init__(self, time, word):
value = float(time[1:3])*60 + float(time[4:])
self.time = value
self.word = word
def __repr__(self):
return '<'+str(self.time) + ':' + self.word+'>'
def __gt__(self, other):
return self.time > other.time
class LyricAnalysis:
def __init__(self, name):
# 歌名
self.name = name
self.__all_lyrics = []
def get_word(self, time):
# =======解析歌詞文件======
if not self.__all_lyrics:
print('解析歌詞')
with open('files/'+self.name) as f:
while True:
line = f.readline()
if not line:
break
lines = line.split(']')
word = lines[-1]
for t in lines[:-1]:
lyric = Lyric(t, word)
self.__all_lyrics.append(lyric)
# 排序
self.__all_lyrics.sort(reverse=True)
# ==========獲取歌詞==========
for lyric in self.__all_lyrics:
if lyric.time <= time:
return lyric.word
ly = LyricAnalysis('藍(lán)蓮花')
print('===:',ly.get_word(123))
print('===:',ly.get_word(10))
print('===:',ly.get_word(89))
ly2 = LyricAnalysis('一首簡(jiǎn)單的歌')
print('!!!:', ly2.get_word(30))
print('!!!:', ly2.get_word(90))
內(nèi)存管理和拷貝
review
1.內(nèi)置類(lèi)屬性: name、doc泪掀、class听绳、dict、module异赫、bases
2.私有化
3.屬性的getter和setter
4.繼承
class 類(lèi)名(父類(lèi)1,父類(lèi)2):
pass
class Person:
def __init__(self, name, age, gender='男'):
# name = '小明', age = 18, gender='男'
self.name = name
self.age = age
self.gender = gender
class Student(Person):
def __init__(self, name, age, study_id, gender='男'):
# name='小明'椅挣, age=18, study_id='001', gender='男'
super(Student, self).__init__(name, age, gender)
self.study_id = study_id
stu = Student('小明', 18, '001')
多繼承
1.多繼承
python中的類(lèi)支持多繼承(讓一個(gè)類(lèi)同時(shí)繼承多個(gè)類(lèi));
多繼承的時(shí)候头岔,子類(lèi)只能繼承第一個(gè)父類(lèi)所有的屬性和方法,后面的父類(lèi)中只有字段和方法可以被繼承
class Animal(object):
num = 100
def __init__(self):
self.age = 0
self.gender = '雌'
@classmethod
def func1(cls):
print('動(dòng)物類(lèi)的類(lèi)方法')
def func2(self):
print('動(dòng)物類(lèi)中的對(duì)象方法')
class Fly(object):
name = '飛行器'
def __init__(self):
self.height = 100
self.time = 5
self.speed = 100
def func2(self):
print('飛行的對(duì)象方法')
class Bird(Animal, Fly):
pass
bird1 = Bird()
# 字段都能繼承
print(Bird.num, Bird.name)
Bird.func1()
bird1.func2()
# print(bird1.age, bird1.gender)
# print(bird1.speed, bird1.height, bird1.time)
運(yùn)算符重載
import copy
1.運(yùn)算符
python中所有的類(lèi)型都是類(lèi)鼠证,所以所有的數(shù)據(jù)都是對(duì)象;
python中使用任意的運(yùn)算符都是在調(diào)用相應(yīng)類(lèi)中的相應(yīng)方法峡竣,每一個(gè)運(yùn)算符對(duì)應(yīng)什么方法是固定的,
某種數(shù)據(jù)是否支持某個(gè)運(yùn)算符操作就看這個(gè)數(shù)據(jù)類(lèi)型中是否實(shí)現(xiàn)了對(duì)應(yīng)的方法
2.運(yùn)算符重載指的是在不同的類(lèi)中實(shí)現(xiàn)同樣的運(yùn)算符對(duì)應(yīng)的函數(shù)
類(lèi)的對(duì)象默認(rèn)情況下只支持: ==, !=
10 * 20
'abc'+'123'
[1, 2] + [2, 3, 4]
# {'a': 10} + {'b': 20}
class Student:
def __init__(self, name, age, score=0):
self.name = name
self.age = age
self.score = score
def __repr__(self):
return '<%s, id:%s>' % (str(self.__dict__)[1:-1], hex(id(self)))
# a+b -> a.__add__(b)
# self -> 當(dāng)前類(lèi)的對(duì)象量九,也是+前面的那個(gè)數(shù)據(jù)
# other -> +后面的那個(gè)數(shù)據(jù), 類(lèi)型根據(jù)運(yùn)算規(guī)則的設(shè)計(jì)可以是任何類(lèi)型的數(shù)據(jù)
def __add__(self, other):
# return self.age + other.age
return self.score + other.score
# return Student(self.name+other.name, self.age+other.age, self.score + other.score)
# return self.score + other
# a*b -> a.__mul__(b)
def __mul__(self, other):
list = []
for _ in range(other):
list.append(copy.copy(self))
return list
# a<b -> a.__lt__(b)
# 注意: <和>符號(hào)只需要重載其中一個(gè)就可以
def __lt__(self, other):
return self.score < other.score
stu1 = Student('小明', 19, 90)
stu2 = Student('小花', 20, 78)
# stu1.__add__(stu2)
print(stu1 + stu2)
# stu1.__add__(100)
# print(stu1 + 100)
print(stu1 * 3)
students = [stu1, stu2, Student('小紅', 12, 100)]
students.sort()
print(students)
print(stu1 < stu2)
print(stu1 > stu2)
淺拷貝和深拷貝
from copy import copy, deepcopy
class Dog:
def __init__(self, name, color):
self.name = name
self.color = color
def __repr__(self):
return '<%s,id: %s>' % (str(self.__dict__)[1:-1], hex(id(self)))
class Person:
def __init__(self, name, age, dog=None):
self.name = name
self.age = age
self.dog = dog
def __repr__(self):
return '<%s,id: %s>' % (str(self.__dict__)[1:-1], hex(id(self)))
p1 = Person('小明', 18, Dog('大黃', '黃色'))
1.直接賦值
將變量中的地址直接賦給新的變量适掰;賦值后兩個(gè)變量的地址相同
p2 = p1
print(id(p1), id(p2)) # 4537270848 4537270848
p1.name = '小花'
print(p1.name, p2.name) # 小花 小花
p2.dog.color = '綠色'
print(p1.dog.color, p2.dog.color) # 綠色 綠色
2.拷貝
不管是淺拷貝還是深拷貝都會(huì)對(duì)原數(shù)據(jù)進(jìn)行賦值產(chǎn)生新的地址
list1 = [1, 2, 3]
list2 = copy(list1)
list3 = deepcopy(list1)
print(id(list1), id(list2), id(list3))
list1.append(100)
print(list2, list3)
p3 = copy(p1)
p4 = deepcopy(p1)
print(id(p1), id(p3), id(p4))
p1.name = '小紅'
print(p3.name, p4.name)
3.淺拷貝
字符串、列表和元祖的切片;對(duì)象.copy(); copy模塊中的copy方法都是淺拷貝
淺拷貝指拷貝當(dāng)前對(duì)象荠列,不會(huì)對(duì)子對(duì)象進(jìn)行拷貝
print('==========淺拷貝=========')
p3 = copy(p1)
print(id(p1), id(p3)) # 4401914600 4401916336
print(id(p1.dog), id(p3.dog)) # 4567065152 4567065152
p1.name = 'Tom'
print(p1.name, p3.name) # Tom 小紅
p1.dog.color = '紅色'
print(p1.dog.color, p3.dog.color) # 紅色 紅色
4.深拷貝
copy模塊中的deepcopy方法是深拷貝
print('==========深拷貝=========')
p4 = deepcopy(p1)
print(id(p1), id(p4)) # 4486472592 4486474384
print(id(p1.dog), id(p4.dog)) # 4485063960 4486474552
p1.name = 'Bob'
print(p1.name, p4.name) # Bob Tom
p1.dog.color = '橙色'
print(p1.dog.color, p4.dog.color) # 橙色 紅色
練習(xí):
a = ['color', 'height', 'background']
b = [a, 'aaa', 'bbb']
c1 = b
c2 = copy(b)
c3 = deepcopy(b)
a[-1] = ['BG']
b.append('ccc')
# b = [['color', 'height', 'background'], 'aaa', 'bbb']
# 問(wèn)題: print(c1), print(c2), print(c3)的結(jié)果分別是
# c1 = [['color', 'height', ['BG']], 'aaa', 'bbb', 'ccc']
# c2 = [['color', 'height', ['BG']], 'aaa', 'bbb']
# c3 = [['color', 'height', 'background'], 'aaa', 'bbb']
print(c1)
print(c2)
print(c3)
枚舉
from enum import Enum, unique
枚舉值的特點(diǎn):
1.可以通過(guò)有意義的屬性名直接顯示數(shù)據(jù)
2.每個(gè)數(shù)據(jù)的值不能修改
3.可以做到不同的數(shù)據(jù)的值唯一
@unique
class PokerNum(Enum):
J = 11
Q = 12
K = 13
A = 1
class Color:
RED = (255, 0, 0)
print(PokerNum.J)
print(PokerNum.K.value, PokerNum.J.value > PokerNum.Q.value)
內(nèi)存管理
from copy import copy, deepcopy
from sys import getrefcount
1.內(nèi)存的開(kāi)辟
內(nèi)存區(qū)間分為棧區(qū)間和堆區(qū)間类浪;棧區(qū)間的內(nèi)存自動(dòng)開(kāi)辟自動(dòng)釋放,堆區(qū)間的內(nèi)存需要程序員手動(dòng)開(kāi)辟肌似,手動(dòng)釋放费就;
但是python已經(jīng)將堆區(qū)間內(nèi)存的開(kāi)辟和釋放自動(dòng)化
a.當(dāng)每次給變量賦值的時(shí)候,系統(tǒng)會(huì)先在堆區(qū)間中開(kāi)辟空間將數(shù)據(jù)存起來(lái)川队,然后再將數(shù)據(jù)在堆中的地址存到變量中力细,變量存在棧區(qū)間;
b.數(shù)字和字符串?dāng)?shù)據(jù)在開(kāi)辟空間的時(shí)候,會(huì)先檢測(cè)內(nèi)存中之前是否已經(jīng)有這個(gè)數(shù)據(jù)固额,
如果有就直接使用之前的數(shù)據(jù)眠蚂,沒(méi)有才開(kāi)辟新的空間保存數(shù)據(jù)
a = [1, 2, 3, [1, 2]] # [1, 2, 0xccc]
b = [1, 2, 3, [1, 2]] # [1, 2, 0xccc]
print(id(a), id(b)) # 4484287176 4484285768
print(id(a[3]), id(b[3]))
print(id(a[0]), id(b[0]), id(a[3][0]), id(b[3][0])) # 4449482688 4449482688 4449482688 4449482688
a1 = 100
b1 = 100
print(id(a1), id(b1)) # 4480746528 4480746528
a2 = 'hello'
b2 = 'hello'
print(id(a2), id(b2)) # 4492420184 4492420184
a3 = {'a': 10}
b3 = {'a': 10}
print(a3 == b3) # True
print(a3 is b3) # False
a4 = 200
b4 = deepcopy(a4)
print(id(a4), id(b4)) # 4367007904 4367007904
a5 = '安徽省打發(fā)卡機(jī)阿哈薩克阿按時(shí)發(fā)阿薩德回復(fù)杰卡斯發(fā)是打飛機(jī)啊山東礦機(jī)返回撒嬌快遞費(fèi)哈薩克京東方撒地方'
b5 = '安徽省打發(fā)卡機(jī)阿哈薩克阿按時(shí)發(fā)阿薩德回復(fù)杰卡斯發(fā)是打飛機(jī)啊山東礦機(jī)返回撒嬌快遞費(fèi)哈薩克京東方撒地方'
print(id(a5), id(b5)) # 4338364064 4338364064
2.內(nèi)存的釋放
棧區(qū)間:全局棧區(qū)間在程序結(jié)束后銷(xiāo)毀,函數(shù)棧區(qū)間在函數(shù)調(diào)用結(jié)束后銷(xiāo)毀(自動(dòng))
堆區(qū)間: 看一個(gè)對(duì)象是否銷(xiāo)毀斗躏,就看這個(gè)對(duì)象的引用計(jì)數(shù)是否為0逝慧;如果一個(gè)對(duì)象的引用為0,這個(gè)對(duì)象就會(huì)銷(xiāo)毀;
(垃圾回收機(jī)制)
注意: python中針對(duì)對(duì)象的循環(huán)引用已經(jīng)做了處理瑟捣,程序員不需要寫(xiě)額外的代碼來(lái)解決循環(huán)引用問(wèn)題
a6 = {'name': '小明', 'age': 18}
b6 = a6
print(getrefcount(a6))
b6 = 100
print(getrefcount(a6))
del a6
print(getrefcount(b6))
class Person:
def __init__(self, name):
self.name = name
self.dog = None
class Dog:
def __init__(self, name):
self.name = name
self.owner = None
dog1 = Dog('大黃')
p1 = Person('小明')
p1.dog = dog1
dog1.owner = p1
del dog1
del p1
撲克洗牌發(fā)牌
from enum import Enum,unique
from random import shuffle
# 1.寫(xiě)一個(gè)撲克類(lèi), 要求擁有發(fā)牌和洗牌的功能(具體的屬性和其他功能自己根據(jù)實(shí)際情況發(fā)揮)
@unique
class PokerNum(Enum):
J = 11, 'J', 11
Q = 12, 'Q', 12
K = 13, 'K', 13
A = 1, 'A', 14
TWO = 2, '2', 15
BigJoker = 15, '大王', 17
SmallJoker = 14, '小王', 16
@unique
class PokerColor(Enum):
Club = '?'
Diamond = '?'
Heart = '?'
Spade = '?'
Space = ''
class Poker:
def __init__(self, num: PokerNum, color: PokerColor):
self.num = num
self.color = color
def __repr__(self):
if isinstance(self.num, PokerNum):
return str(self.color.value) + str(self.num.value[1])
return str(self.color.value)+str(self.num)
def __gt__(self, other):
if isinstance(self.num, PokerNum):
a = self.num.value[2]
else:
a = self.num
if isinstance(other.num, PokerNum):
b = other.num.value[2]
else:
b = other.num
return a > b
class Game:
# 斗地主
def __init__(self):
pokers = []
colors = [PokerColor.Club, PokerColor.Diamond, PokerColor.Heart, PokerColor.Spade]
nums = [PokerNum.A, PokerNum.TWO, 3, 4, 5, 6, 7, 8, 9, 10, PokerNum.J, PokerNum.Q, PokerNum.K]
for num in nums:
for color in colors:
p = Poker(num, color)
pokers.append(p)
pokers.extend([Poker(PokerNum.SmallJoker, PokerColor.Space), Poker(PokerNum.BigJoker, PokerColor.Space)])
self.pokers = pokers
self.pokers_iter = iter(self.pokers)
# 洗牌
def shuffling(self):
shuffle(self.pokers)
self.pokers_iter = iter(self.pokers)
# 斗地主發(fā)牌方式
def deal1(self):
ps1 = []
ps2 = []
ps3 = []
for _ in range(17):
ps1.append(next(self.pokers_iter))
ps2.append(next(self.pokers_iter))
ps3.append(next(self.pokers_iter))
ps1.sort(reverse=True)
ps2.sort(reverse=True)
ps3.sort(reverse=True)
return ps1, ps2, ps3, list(self.pokers_iter)
game1 = Game()
game1.shuffling()
p1, p2, p3, di = game1.deal1()
print(p1)
print(p2)
print(p3)
print('底牌:', di)
歌詞解釋器
# (嘗試)寫(xiě)一個(gè)類(lèi)栅干,其功能是:1.解析指定的歌詞文件的內(nèi)容 2.按時(shí)間顯示歌詞
# 提示:歌詞文件的內(nèi)容一般是按下面的格式進(jìn)行存儲(chǔ)的迈套。
# 歌詞前面對(duì)應(yīng)的是時(shí)間,在對(duì)應(yīng)的時(shí)間點(diǎn)可以顯示對(duì)應(yīng)的歌詞
class Lyric:
def __init__(self, time, word):
value = float(time[1:3])*60 + float(time[4:])
self.time = value
self.word = word
def __repr__(self):
return '<'+str(self.time) + ':' + self.word+'>'
def __gt__(self, other):
return self.time > other.time
class LyricAnalysis:
def __init__(self, name):
# 歌名
self.name = name
self.__all_lyrics = []
def get_word(self, time):
# =======解析歌詞文件======
if not self.__all_lyrics:
print('解析歌詞')
with open('files/'+self.name) as f:
while True:
line = f.readline()
if not line:
break
lines = line.split(']')
word = lines[-1]
for t in lines[:-1]:
lyric = Lyric(t, word)
self.__all_lyrics.append(lyric)
# 排序
self.__all_lyrics.sort(reverse=True)
# ==========獲取歌詞==========
for lyric in self.__all_lyrics:
if lyric.time <= time:
return lyric.word
ly = LyricAnalysis('藍(lán)蓮花')
print('===:',ly.get_word(123))
print('===:',ly.get_word(10))
print('===:',ly.get_word(89))
ly2 = LyricAnalysis('一首簡(jiǎn)單的歌')
print('!!!:', ly2.get_word(30))
print('!!!:', ly2.get_word(90))