Day16總結(jié)

第一節(jié):運(yùn)算符的重載

python中所有的數(shù)據(jù)類型都是,數(shù)據(jù)都是對象。
所有的運(yùn)算符對應(yīng)的操作,本質(zhì)都是在調(diào)用數(shù)據(jù)類型對應(yīng)的魔法方法。(每個(gè)運(yùn)算符都對應(yīng)一個(gè)固定的魔法方法)

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__)

    # 重載加法預(yù)算符
    # self +  other = return 返回值
    def __add__(self, other):
        return self.age + other.age

    # 重載乘法運(yùn)算符
    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

    # 注意: >和<只需要重載一個(gè)


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)

練習(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> ]

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('======直接賦值=====')

#第二節(jié):淺拷貝和深拷貝

1.一個(gè)變量直接給另外一個(gè)變量賦值:直接將地址賦值,賦完后兩個(gè)變量指向同一塊內(nèi)存區(qū)域,并且相互影響

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

2.淺拷貝和深拷貝(面試點(diǎn)!)

拷貝原理: 將被拷貝的對象復(fù)制一份,產(chǎn)生一個(gè)新的數(shù)據(jù),然后將新的數(shù)據(jù)的地址返回

a.淺拷貝

  1. 列表或字典的copy方法是淺拷貝、切片也是淺拷貝
  2. copy.copy(對象) - 復(fù)制指定的對象,產(chǎn)生一個(gè)新的對象(不會(huì)復(fù)制子對象)

b.深拷貝
copy.deepcopy(對象) - 復(fù)制指定的對象,產(chǎn)生一個(gè)新的對象。如果這個(gè)對象中有其他的對象,子對象也會(huì)被復(fù)制

print('======淺拷貝=====')
dog1 = Dog('財(cái)財(cái)')
stu2 = Student('Lisa', 18, 60)
stu2.dog = dog1

stu4 = copy.copy(stu2)

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


print('======深拷貝=====')
dog1 = Dog('財(cái)財(cái)')
stu2 = Student('Lisa', 18, 60)
stu2.dog = dog1

stu4 = copy.deepcopy(stu2)

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


第三節(jié) 數(shù)據(jù)的存儲(chǔ)和內(nèi)存開辟和釋放

from sys import getrefcount

1.數(shù)據(jù)的存儲(chǔ)(內(nèi)存開辟)

python的變量都存儲(chǔ)在棧區(qū)間,對象都在堆區(qū)間。

聲明變量或者給變量賦值,是先在內(nèi)存(堆)中開辟存儲(chǔ)數(shù)據(jù),然后將數(shù)據(jù)地址保存在變量中。
但是數(shù)字和字符串特殊,如果是用數(shù)字或者字符串給變量賦值,不會(huì)直接開辟空間保存數(shù)據(jù),
而是先在內(nèi)存檢測這個(gè)數(shù)據(jù)之前是否已經(jīng)存儲(chǔ)過,如果已經(jīng)存儲(chǔ)直接用上次保存的數(shù)據(jù),沒有存儲(chǔ)才會(huì)開辟新的空間保存數(shù)據(jù)

2. 內(nèi)存的釋放

1)引用計(jì)數(shù)
python每個(gè)對象都有一個(gè)屬性叫引用計(jì)數(shù),用來保存當(dāng)前對象的引用的個(gè)數(shù)。

python中的垃圾回收機(jī)制來判斷一個(gè)對象是否銷毀,就看這個(gè)對象的引用計(jì)數(shù)是否為零,如果為零就會(huì)被銷毀。



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('==============引用計(jì)數(shù)===========')
list1 = [1, 2]
print(getrefcount(list1))

# def yt_getrefcount(obj):
#     # obj = list1
#     return 獲取obj對應(yīng)的數(shù)據(jù)的引用個(gè)數(shù)

# yt_getrefcount(list1)

# 讓引用計(jì)數(shù)增加
list2 = list1
print(getrefcount(list1))
dict1 = {'a': list2}
print(getrefcount(list1))

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

# 讓引用計(jì)數(shù)減少
print(getrefcount(list1))
list2 = 100
print(getrefcount(list1))
del dict1['a']
print(getrefcount(list1))
del list1
# print(getrefcount(list1))
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 2019-05-07 1.運(yùn)算符重載 python中所有的數(shù)據(jù)類型都是,數(shù)據(jù)都是對象。 所有的運(yùn)算符對應(yīng)的操作,本...
    快請輸入昵稱吧閱讀 161評論 0 0
  • 類的繼承 python中類支持繼承,并且支持多繼承 1.什么是繼承 父類(超類):被繼承的類子類:去繼承父類的類繼...
    Heyjoky閱讀 250評論 0 2
  • 1.運(yùn)算符的重載 python中的數(shù)據(jù)類型都是類,數(shù)據(jù)都是對象所有運(yùn)算符對應(yīng)的操作,都是在調(diào)用數(shù)據(jù)類型對應(yīng)的魔法方...
    XXXXStone閱讀 160評論 0 0
  • 一、運(yùn)算符重載 通過在類中實(shí)現(xiàn)運(yùn)算符對應(yīng)的魔法方法,來讓類的對象支持相關(guān)運(yùn)算符的操作 python中所有的數(shù)據(jù)類型...
    墨2019418閱讀 198評論 0 0
  • python中所有的數(shù)據(jù)類型都是類,數(shù)據(jù)都是對象所有的運(yùn)算符對應(yīng)的操作,本質(zhì)都是在調(diào)用數(shù)據(jù)類型對應(yīng)的魔法方法。 每...
    __e145閱讀 183評論 0 0

友情鏈接更多精彩內(nèi)容