1.多繼承
python中的類支持多繼承姐叁,也就是讓一個類有多個父類够掠。
class Animal(object):
num = 100
def __init__(self):
self.age = 0
self.gender = '雌'
@classmethod
def func2(cls):
print('動物類的類方法果漾。')
class Fly(object):
name = '飛'
def __init__(self):
self.height = 100
self.time = 5
self.speed = 100
def func2(self):
print('飛行的對象的方法佛猛。')
class Bird(Animal, Fly):
pass
bird1 = Bird()
print(Bird().num, Bird().name) # 100 飛
print(Bird.num, Bird.name) # 100 飛
print(bird1.num, bird1.name) # 100 飛
print(bird1.gender) # 雌
print(bird1.speed) # AttributeError: 'Bird' object has no attribute 'speed'
當子類在繼承多個父類的時候祠锣,只繼承了第一個父類的屬性(Animal),所以子類在調(diào)用speed屬性的時候就會報錯。
運算符
python中所有的類型都是類泵三,所以所有的數(shù)據(jù)都是對象耕捞,python中使用的任意運算符都是在調(diào)用相應(yīng)類中的相應(yīng)的方法。每一個運算符對應(yīng)的什么方法都是固定的烫幕,某種數(shù)據(jù)是否支持該運算符就看這個數(shù)據(jù)類型中是否實現(xiàn)了對應(yīng)的方法俺抽。
默認情況下,類的對象支持 ==较曼,!=
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 -> 當前類的對象磷斧,也是+前面的那個數(shù)據(jù)
# other -> +后面的那個數(shù)據(jù), 類型根據(jù)運算規(guī)則的設(shè)計可以是任何類型的數(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)
# 注意: <和>符號只需要重載其中一個就可以
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 * 2) # [<'name': '小明', 'age': 19, 'score': 90, id:0x16c61f4ed68>,
<'name': '小明', 'age': 19, 'score': 90, id:0x16c61f4ea20>]
# students = [stu1, stu2, Student('小紅', 12, 100)]
# students.sort()
# print(students)
#
# print(stu1 < stu2)
# print(stu1 > stu2)
淺拷貝和深拷貝
1.直接賦值
將變量中的地址直接賦給新的變量,賦值后兩個變量的地址相同
p1 = 20
p2 = p1
print(id(p1), id(p2)) #4537270848 4537270848
2.拷貝
不管是淺拷貝還是深拷貝都是會對原數(shù)據(jù)進行賦值產(chǎn)生新的地址捷犹,淺拷貝只是拷貝當前對象弛饭,不會拷貝當前對象中的子對象,深拷貝則是不僅拷貝當前對象萍歉,也拷貝當前對象中的所有子對象侣颂。
import copy
list1 = [1, 2, 3]
list2 = copy.copy(list1)
list3 = copy.deepcopy(list1)
print(id(list1), id(list2), id(list3))
3.淺拷貝
字符串、列表和元組的切片翠桦;對象.copy();copy模塊中的copy方法都是淺拷貝横蜒,淺拷貝只拷貝當前對象胳蛮,不會對子對象進行拷貝
4.深拷貝
深拷貝就是copy模塊中的deepcopy方法
# 練習(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']
# 問題: 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)
枚舉
枚舉值的特點:
1)可以通過有意義的屬性名直接顯示數(shù)據(jù)
2)每個數(shù)據(jù)的值不能修改
3)可以做到不同的數(shù)據(jù)值唯一
from enum import Enum, unique
@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)存管理
1.內(nèi)存的開辟
內(nèi)存區(qū)間分為棧區(qū)間和堆區(qū)間销凑,棧區(qū)間的內(nèi)存自動開辟自動釋放,堆區(qū)間的內(nèi)存需要手動來開辟以及釋放仅炊;但是python已經(jīng)將堆區(qū)間內(nèi)存的開辟和釋放自動化
a.在python中當每次給變量賦值的時候斗幼,系統(tǒng)會先在堆區(qū)間中開辟空間將數(shù)據(jù)存起來,然后再將數(shù)據(jù)在堆區(qū)間的地址存到變量中抚垄,變量存儲在棧區(qū)間蜕窿。
b.數(shù)字和字符串在開辟空間的時候谋逻,會先檢查內(nèi)存中之前是否存儲過該數(shù)據(jù),如果有就直接使用這個數(shù)據(jù)桐经,沒有則開辟新的空間來保存這個數(shù)據(jù)
2.內(nèi)存的釋放
棧區(qū)間:全局棧區(qū)間在程序結(jié)束后自動銷毀毁兆,函數(shù)棧區(qū)間在函數(shù)調(diào)用結(jié)束后自動銷毀。
堆區(qū)間:看一個對象是否被銷毀阴挣,就看這個對象的引用計數(shù)是否為0气堕,如果引用為0,那么這個對象就會被銷毀(垃圾回收機制)
注意:python中針對對象的循環(huán)引用已經(jīng)做了處理畔咧,程序員不需要寫額外的代碼來解決循環(huán)引用的問題
# 面試題:== 和 is的區(qū)別?
# 補充: is的使用 - 判斷兩個數(shù)據(jù)的地址是否一樣
list1 = [1, 2, 3]
list2 = [1, 2, 3]
list3 = list1
print(list1 == list2, list1 == list3) # True, True
print(list1 is list2, list1 is list3) # False, True