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.解析歌词文件中的内容
with open('files/%s.txt' % self.name, 'r', encoding='utf-8') as f:
while True:
# 读一行内容
line = f.readline()
# 切字符串
lines = line.split(']')
# 遍历时间创建歌词对象
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.当前歌的歌词按时间排序
self.all_lyric.sort(reverse=True, key=lambda item: item.time)
# print(self.all_lyric)
# 3.根据时间找对应的歌词
for item in self.all_lyric:
if item.time < time:
return item.word
return self.name
l1 = LyricAnalysis('蓝莲花')
print(l1.get_world(10))
"""
python中所有数据类型都是类,数据都是对象。
所有的运算符对应的操作本质都是在调用数据类型对应的魔法方法。(没个运算符都对应一个魔法方法)
"""
class Student:
def init(self, name, age=0, score=0):
self.name = name
self.age = age
self.score = score
# 重载加法运算符
# self + other = return 返回值
def __add__(self, other):
return self.age + other.age
# 重载乘法运算符
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
# 注意: >和<只需要重载一个
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)
练习: 让Student的对象支持乘法运算,运算规则是:
<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.一个变量直接给另外一个变量赋值:直接将地址赋值,赋完后两个变量指向同一块内存区域,并且相互影响
stu2 = Student('Lisa', 18, 69)
stu3 = stu2
print(id(stu2), id(stu3))
stu2.age = 28
print(stu3.dict)
2.浅拷贝和深拷贝(面试点!!)
"""
拷贝原理:将被拷贝的对象复制一份,产生一个新的数据,然后将新的数据的地址返回
a.浅拷贝
1)列表或字典的copy方法是浅拷贝、切片也是浅拷贝
2)copy.copy(对象) - 复制指定的对象,产生一个新的对象(不会复制子对象)
b.深拷贝
copy.deepcopy(对象) - 复制指定的对象,产生一个新的对象。如果这个对象中有其他的对象,子对象也会被复制
"""
print('======浅拷贝=======')
stu2 = Student('Lisa', 18, 69)
stu3 = stu2
print("======深拷贝=======")
stu2 = Student('Lisa', 18, 69)
stu4 = copy.copy(stu2)
1.数据的存储(内存开辟)
"""
python的变量都存储在栈区间,对象都在堆区间。
声明变量或者给变量赋值,是先在内存中(堆)中开辟存储数据,然后将数据地址保存在变量中。
但是数字和字符串特殊,如果是用数字或者字符串给变量赋值,他不会直接开辟空间保存数据,
而是先在内存中检查这个数据之前是否已经存储过,如果已经存储,直接用上次保存的数据,没有存储才会开辟新的空间保存数据
"""
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.内存的释放
"""
1)引用计数
python中每个对象都有一个属性叫引用计数,用来保存当前对象的引用的个数。
python中的垃圾回收机制来判断一个对象是否销毁,就看这个对象的引用技术是否为零,如果为零就被销毁
"""
from sys import getrefcount
print('=========引用计数========')
list1 = [1, 2]
print(getrefcount(list1))
让引用计数增加
list2 = list1
print(getrefcount(list1))
dict1 = {'a': list2}
print(getrefcount(list1))
num = 100
print(getrefcount(num))
num1 = 100
print(getrefcount(num1))
让引用计数减少
print(getrefcount(list1))
list2 = 100
print(getrefcount(list1))
del dict1['a']
print(getrefcount(list1))