class Lyric:
def init(self, word):
self._time = 0
self.word = word
@property
def time(self):
return self._time
@time.setter
def time(self, value):
# value = '[00:22:34]'
fen = float(value[1:3])
miao = float(value[4:])
self._time = fen * 60 + miao
def __repr__(self):
return str(self.__dict__)
class LyricAnalysis:
def __init__(self, name):
self.name = name
self.all_lyric = []
# def analysis_file(self):
# 需要: 歌名
def get_world(self, time):
if not self.all_lyric:
# 1.解析歌詞文件中的內(nèi)容
with open('files/%s.txt' % self.name, 'r', encoding='utf-8') as f:
while True:
# 讀一行內(nèi)容
line = f.readline()
# 切字符串
lines = line.split(']')
# 遍歷時(shí)間創(chuàng)建歌詞對象
for time_str in lines[:-1]:
lyric_obj = Lyric(lines[-1])
lyric_obj.time = time_str
# print(lyric_obj.__dict__)
self.all_lyric.append(lyric_obj)
if not line:
break
# 2.當(dāng)前歌的歌詞按時(shí)間排序
self.all_lyric.sort(reverse=True, key=lambda item: item.time)
# print(self.all_lyric)
# 3.根據(jù)時(shí)間找對應(yīng)的歌詞
for item in self.all_lyric:
if item.time < time:
return item.word
return self.name
l1 = LyricAnalysis('藍(lán)蓮花')
print(l1.get_world(10))
"""
python中所有數(shù)據(jù)類型都是類爽航,數(shù)據(jù)都是對象揽祥。
所有的運(yùn)算符對應(yīng)的操作本質(zhì)都是在調(diào)用數(shù)據(jù)類型對應(yīng)的魔法方法。(沒個(gè)運(yùn)算符都對應(yīng)一個(gè)魔法方法)
"""
class Student:
def init(self, name, age=0, score=0):
self.name = name
self.age = age
self.score = score
# 重載加法運(yùn)算符
# self + other = return 返回值
def __add__(self, other):
return self.age + other.age
# 重載乘法運(yùn)算符
def __mul__(self, other: int):
return self.score * other
# def __gt__(self, other):
# return self.score > other.score
# def __repr__(self):
# return str(all_student.__dict__)
def __lt__(self, other):
return self.score < other.score
# 注意: >和<只需要重載一個(gè)
stu1 = Student('小明', 19, 22)
stu2 = Student('小米', 44, 45)
print(stu1 + stu2)
print(stu1 * 10)
print(stu1 < stu2)
all_student = [stu1, stu2, Student('小號', 34, 43), Student('小米', 11, 22)]
print(all_student)
練習(xí): 讓Student的對象支持乘法運(yùn)算贷揽,運(yùn)算規(guī)則是:
<name: 張三集乔, age:10, score:0> * 3 = [<name: 張三, age:10, score:0>, <name: 張三, age:10, score:0>, <name: 張三, age:10, score:0>]
class Student:
def init(self, name='張三', age=10, score=0):
self.name = name
self.age = age
self.score = score
def mul(self, other):
return self.name * 3, self.age * 3, self.score * 3
import copy
class Dog:
def init(self, name, color='黃色'):
self.name = name
self.color = color
class Student:
def init(self, name, age=0, score=0):
self.name = name
self.age = age
self.score = score
self.dog = None
def __repr__(self):
return '<' + str(self.__dict__)[1:-1] + '>'
def __mul__(self, other):
result = []
for _ in range(other):
result.append(self)
return result
stu1 = Student('張三', 18, 90)
print(stu1)
result = stu1 * 2
print(result)
print('===================深拷貝和淺拷貝=================')
print("直接賦值")
1.一個(gè)變量直接給另外一個(gè)變量賦值:直接將地址賦值,賦完后兩個(gè)變量指向同一塊內(nèi)存區(qū)域,并且相互影響
stu2 = Student('Lisa', 18, 69)
stu3 = stu2
print(id(stu2), id(stu3))
stu2.age = 28
print(stu3.dict)
2.淺拷貝和深拷貝(面試點(diǎn)!!)
"""
拷貝原理:將被拷貝的對象復(fù)制一份闸昨,產(chǎn)生一個(gè)新的數(shù)據(jù),然后將新的數(shù)據(jù)的地址返回
a.淺拷貝
1)列表或字典的copy方法是淺拷貝、切片也是淺拷貝
2)copy.copy(對象) - 復(fù)制指定的對象薄风,產(chǎn)生一個(gè)新的對象(不會復(fù)制子對象)
b.深拷貝
copy.deepcopy(對象) - 復(fù)制指定的對象饵较,產(chǎn)生一個(gè)新的對象。如果這個(gè)對象中有其他的對象遭赂,子對象也會被復(fù)制
"""
print('======淺拷貝=======')
stu2 = Student('Lisa', 18, 69)
stu3 = stu2
print("======深拷貝=======")
stu2 = Student('Lisa', 18, 69)
stu4 = copy.copy(stu2)
1.數(shù)據(jù)的存儲(內(nèi)存開辟)
"""
python的變量都存儲在棧區(qū)間循诉,對象都在堆區(qū)間。
聲明變量或者給變量賦值撇他,是先在內(nèi)存中(堆)中開辟存儲數(shù)據(jù)茄猫,然后將數(shù)據(jù)地址保存在變量中。
但是數(shù)字和字符串特殊,如果是用數(shù)字或者字符串給變量賦值,他不會直接開辟空間保存數(shù)據(jù)困肩,
而是先在內(nèi)存中檢查這個(gè)數(shù)據(jù)之前是否已經(jīng)存儲過,如果已經(jīng)存儲划纽,直接用上次保存的數(shù)據(jù),沒有存儲才會開辟新的空間保存數(shù)據(jù)
"""
list1 = [1, 2]
list2 = [1, 2]
print(id(list1), id(list2))
num1 = {}
num2 = {}
print(id(num1), id(num2))
num3 = 99999999999999999999999999999999999999
num4 = 99999999999999999999999999999999999999
print(id(num3), id(num4))
2.內(nèi)存的釋放
"""
1)引用計(jì)數(shù)
python中每個(gè)對象都有一個(gè)屬性叫引用計(jì)數(shù),用來保存當(dāng)前對象的引用的個(gè)數(shù)。
python中的垃圾回收機(jī)制來判斷一個(gè)對象是否銷毀,就看這個(gè)對象的引用技術(shù)是否為零,如果為零就被銷毀
"""
from sys import getrefcount
print('=========引用計(jì)數(shù)========')
list1 = [1, 2]
print(getrefcount(list1))
讓引用計(jì)數(shù)增加
list2 = list1
print(getrefcount(list1))
dict1 = {'a': list2}
print(getrefcount(list1))
num = 100
print(getrefcount(num))
num1 = 100
print(getrefcount(num1))
讓引用計(jì)數(shù)減少
print(getrefcount(list1))
list2 = 100
print(getrefcount(list1))
del dict1['a']
print(getrefcount(list1))