Day16总结

第一节:运算符的重载

python中所有的数据类型都是,数据都是对象。
所有的运算符对应的操作,本质都是在调用数据类型对应的魔法方法。(每个运算符都对应一个固定的魔法方法)

class Student(object):
    def __init__(self, name, age=0, score=0):
        self.name = name
        self.age = age
        self.score = score

    def __repr__(self):
        return str(self.__dict__)

    # 重载加法预算符
    # self +  other = return 返回值
    def __add__(self, other):
        return self.age + other.age

    # 重载乘法运算符
    def __mul__(self, other):
        return self.score * other

    # >
    def __gt__(self, other):
        return self.score > other.score

    # <
    # def __lt__(self, other):
    #     return self.score < other.score

    # 注意: >和<只需要重载一个


stu1 = Student('小明', 18, 60)
stu2 = Student('小花', 22, 80)

print(stu1 + stu2)
print(stu1 * 10)
print(stu1 > stu2)
print(stu1 < stu2)


all_students = [stu1, stu2, Student('小小', 17, 55), Student('xiaohong', 25, 70)]
all_students.sort()
print(all_students)

练习: 让Student的对象支持乘法运算,运算规则是:
* 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):
        # self = stu1, other = 2
        result = []
        for _ in range(other):
            result.append(self)
        return result


stu1 = Student('张三', 18, 90)
print(stu1)

result = stu1 * 2
print(result)

stu1.name = '小明'
print(result)

result[0].name = '小花'
print(stu1, result)

print('=====================深拷贝和浅拷贝====================')
print('======直接赋值=====')

#第二节:浅拷贝和深拷贝

1.一个变量直接给另外一个变量赋值:直接将地址赋值,赋完后两个变量指向同一块内存区域,并且相互影响

stu2 = Student('Lisa', 18, 60)
stu3 = stu2
print(id(stu3), id(stu2))
stu2.age = 28
print(stu3)

2.浅拷贝和深拷贝(面试点!)

拷贝原理: 将被拷贝的对象复制一份,产生一个新的数据,然后将新的数据的地址返回

a.浅拷贝

  1. 列表或字典的copy方法是浅拷贝、切片也是浅拷贝
  2. copy.copy(对象) - 复制指定的对象,产生一个新的对象(不会复制子对象)

b.深拷贝
copy.deepcopy(对象) - 复制指定的对象,产生一个新的对象。如果这个对象中有其他的对象,子对象也会被复制

print('======浅拷贝=====')
dog1 = Dog('财财')
stu2 = Student('Lisa', 18, 60)
stu2.dog = dog1

stu4 = copy.copy(stu2)

print('stu4:', stu4)
stu2.name = '小花'
print(stu2, stu4)


print('======深拷贝=====')
dog1 = Dog('财财')
stu2 = Student('Lisa', 18, 60)
stu2.dog = dog1

stu4 = copy.deepcopy(stu2)

print('stu4:', stu4)
stu2.name = '小花'
print(stu2, stu4)


第三节 数据的存储和内存开辟和释放

from sys import getrefcount

1.数据的存储(内存开辟)

python的变量都存储在栈区间,对象都在堆区间。

声明变量或者给变量赋值,是先在内存(堆)中开辟存储数据,然后将数据地址保存在变量中。
但是数字和字符串特殊,如果是用数字或者字符串给变量赋值,不会直接开辟空间保存数据,
而是先在内存检测这个数据之前是否已经存储过,如果已经存储直接用上次保存的数据,没有存储才会开辟新的空间保存数据

2. 内存的释放

1)引用计数
python每个对象都有一个属性叫引用计数,用来保存当前对象的引用的个数。

python中的垃圾回收机制来判断一个对象是否销毁,就看这个对象的引用计数是否为零,如果为零就会被销毁。



list1 = [1, 2]
list2 = [1, 2]
print(id(list1), id(list2))

num1 = 10
num2 = 10
print(id(num1), id(num2))


class Student:
    def __init__(self):
        self.name = '张三'


print('==============引用计数===========')
list1 = [1, 2]
print(getrefcount(list1))

# def yt_getrefcount(obj):
#     # obj = list1
#     return 获取obj对应的数据的引用个数

# yt_getrefcount(list1)

# 让引用计数增加
list2 = list1
print(getrefcount(list1))
dict1 = {'a': list2}
print(getrefcount(list1))

#
# num = 100
# print(getrefcount(num))
# num1 = 100
# print(getrefcount(num))

# 让引用计数减少
print(getrefcount(list1))
list2 = 100
print(getrefcount(list1))
del dict1['a']
print(getrefcount(list1))
del list1
# print(getrefcount(list1))

你可能感兴趣的:(Day16总结)