一、多繼承
1.多繼承
python中的類支持多繼承(讓一個類同時繼承多個類)
多繼承的時候,子類只能繼承第一個父類的所有屬性和方法,后面的父類中只有字段和方法被繼承
class Animal(object):
num = 100
def __init__(self):
self.age = 10
self.gender = '公'
@classmethod
def func1(cls):
print('動物類的類方法')
# def func2(self):
# print('動物類的func2')
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)
Bird.func1()
bird1.func2()
print(bird1.age, bird1.gender)
# print(bird1.speed, bird1.height, bird1.time)
# AttributeError: 'Bird' object has no attribute 'speed'
二、運算符重載
1.運算符
- python中所有的類型都是類,所以所有的數(shù)據(jù)都是對象;
- python使用任意的運算符都是在調用相應類中的方法,每一個運算符對應什么方法是固定的
- 某種數(shù)據(jù)是否支持某個運算符操作符就看這個數(shù)據(jù)中是否實現(xiàn)了對應的方法
def func1():
pass
def func1(a):
pass
def func1(a, b): # 只有這個函數(shù)才能被調用,上述兩個都無法調用
pass
2.運算符重載指的是在不同的類中實現(xiàn)同樣的運算符對應的函數(shù)
類的對象默認情況下支持: ==和!=
import copy
10 + 20
'abc' + '123'
[1, 2] + [2, 3, 4]
# {'a': 10} + {'b': 20} # 字典不支持加法
dict
class Student:
def __init__(self, name, age, score=0):
self.name = name
self.age = age
self.score = score
# a + b -> a.__add(b)
# self ->當前對象,也是 + 前面的那個數(shù)據(jù)
# other -> + 后面的那個數(shù)據(jù),類型根據(jù)運算符規(guī)則的設計可以是任何類型的數(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):
list1 = []
for _ in range(other):
list1.append(copy.copy(self))
return list1
# a<b -> a.__lt__(b)
# 注意:'<'和'>'符號只需要重載其中一個就可以
def __lt__(self, other):
return self.score < other.score
def __repr__(self):
return '<' + str(self.__dict__)[1:-1] + ', ' + hex(id(self)) + '>'
stu1 = Student('小明', 19, 90)
stu2 = Student('小花', 20, 78)
# stu1.__add__(stu2)
# print(stu1 + stu2)
# TypeError: unsupported operand type(s) for +: 'Student' and 'Student'
print(stu1 == stu2) # False, object都支持(==)和(!=)
# stu2 = stu1
# print(stu1 == stu2) # True
# print(stu1 > stu2)
# TypeError: '>' not supported between instances of 'Student' and 'Student'
print(stu1 * 3)
students = [stu1, stu2, Student('小紅', 12, 100)]
students.sort()
# TypeError: '<' not supported between instances of 'Student' and 'Student'
print(students)
print(stu2 < stu1)
print(stu1 < stu2)
三、淺拷貝和深拷貝
from copy import copy, deepcopy
class Dog:
def __init__(self, name, color):
self.name = name
self.color = color
def __repr__(self):
return '<%s, id: %s>' % (str(self.__dict__)[1:-1], hex(id(self)))
class Person:
def __init__(self, name, age, dog=None):
self.name = name
self.age = age
self.dog = dog
def __repr__(self):
return '<%s, id: %s>' % (str(self.__dict__)[1:-1], hex(id(self)))
p1 = Person('小明', 18, Dog('大黃', 'yellow'))
1.直接復制
將變量中的地址直接賦值給新的變量,復制后兩個變量的地址相同
p2 = p1
print(p1)
print(p2)
print(id(p1), id(p2))
p1.name = '小花'
print(p2.name, p1.name) # 小花 小花
p2.dog.color = 'green'
print(p1.dog.color, p2.dog.color)
2.拷貝
不管是淺拷貝還是深拷貝都會對原數(shù)據(jù)進行復制產(chǎn)生新的地址
list1 = [1, 2, 3]
list2 = copy(list1)
list3 = deepcopy(list1)
print(id(list1), id(list2), id(list3))
list1.append(100)
print(list2, list3)
p3 = copy(p1)
p4 = deepcopy(p1)
print(id(p1), id(p3), id(p4))
p1.name = '小紅'
print(p3.name, p4.name)
3.淺拷貝
- 字符串、列表和元組的切片;
對象.copy();copy模塊中的copy方法都是淺拷貝 - 淺拷貝只拷貝當前對象,不會對子對象進行拷貝
print('====================淺拷貝======================')
p3 = copy(p1)
print(id(p1), id(p3)) # 38461192 38493256
print(id(p1.dog), id(p3.dog)) # 34725576 34725576
p1.name = 'Tom'
print(p1.name, p3.name) # Tom 小紅
p1.dog.color = 'red'
print(p1.dog.color, p3.dog.color) # red red
4.深拷貝
copy模塊中的deepcopy方法是深拷貝
print('====================深拷貝======================')
p4 = deepcopy(p1)
print(id(p1), id(p4)) # 39633352 39800136
print(id(p1.dog), id(p4.dog)) # 39633288 39800456
p1.name = 'Bob'
print(p1.name, p4.name) # Bob Tom
p1.dog.color = 'orange'
print(p1.dog.color, p4.dog.color) # orange red
練習
a = ['color', 'height', 'background']
b = [a, 'aaa', 'bbb']
c1 = b
c2 = copy(b)
c3 = deepcopy(b)
a[-1] = ['BG']
b.append('ccc')
# 問題:print(c1), print(c2), print(c3)的結果分別是
# b = [['color', 'height', 'BG'], 'aaa', 'bbb', 'ccc']
c1 = [['color', 'height', 'BG'], 'aaa', 'bbb', 'ccc']
c2 = [['color', 'height', 'BG'], 'aaa', 'bbb']
c3 = ['color', 'height', 'background', 'aaa', 'bbb']
四、枚舉
枚舉的特點:
- 可以通過有意義的屬性名直接顯示數(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
# A = 11
# ValueError: duplicate values found in < enum 'PokerNum' >: A -> J
print(PokerNum.J.value + PokerNum.K.value) # 24
print(PokerNum.J.value > PokerNum.K.value) # False
print(PokerNum.J) # PokerNum.J
print(PokerNum.J.value) # 11
# PokerNum.J = 12 # AttributeError: Cannot reassign members.
@unique
class Color(Enum):
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
WHITE = (255, 255, 255)
nums = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
五、內存管理
1.內存開辟
內存區(qū)間分為棧區(qū)間和堆區(qū)間;棧區(qū)間的內存自動開辟自動釋放,堆區(qū)間的內存需要程序員手動開辟,手動釋放;
但是python已經(jīng)將堆區(qū)間內存的開辟和釋放自動化
當每次給變量賦值的時候,系統(tǒng)會在堆區(qū)間中開辟空間將數(shù)據(jù)存起來,然后再將數(shù)據(jù)在堆中的地址存到變量中,變量存在棧區(qū)間;
數(shù)字和字符串數(shù)據(jù)在開辟空間的時候會檢查內存中之前是否已經(jīng)有這個數(shù)據(jù),如果有就直接使用之前的數(shù)據(jù),沒有才開辟新的空間保存數(shù)據(jù)
from copy import copy, deepcopy
from sys import getrefcount
a = [1, 2, 3] # [0xff, 0xee, 0xcc]
b = [1, 2, 3] # [0xff, 0xee, 0xcc]
print(id(a), id(b))
# 41904008 41903752
print(id(a[0]), id(b[0]))
# 8791287558400 8791287558400
a = [1, 2, 3, [1, 2]]
b = [1, 2, 3, [1, 2]]
print(id(a[0]), id(b[0]))
# 8791287558400 8791287558400
print(id(a[3]), id(b[3]))
# 41904008 41903752
print(id(a[3][0]), id(b[3][0]))
# 8791287558400 8791287558400
a1 = 100
b1 = 100
print(id(a1), id(b1))
# 8791287561568 8791287561568
a2 = 'hello'
b2 = 'hello'
print(id(a2), id(b2))
# 34329520 34329520
a3 = {'a': 10}
b3 = {'a': 10}
print(a3 == b3) # True
print(a3 is b3) # False
a4 = 200
b4 = deepcopy(a4)
print(id(a4), id(b4)) # 8791287564768 8791287564768
a5 = 'assecrcrdfseccffffffffffffffffffffffffffffffffff'
b5 = 'assecrcrdfseccffffffffffffffffffffffffffffffffff'
print(id(a5), id(b5)) # 4925680 4925680
2.內存的釋放
棧區(qū)間:全局棧區(qū)間在程序結束后銷毀,函數(shù)棧區(qū)間在函數(shù)調用結束后銷毀(自動)
堆區(qū)間:看一個對象是否銷毀,就看這個對象的引用計數(shù)是否為0,如果一個對象的引用為0,這個對象就會銷毀(垃圾回收機制)
注意:python中針對對象的循環(huán)引用已經(jīng)做了處理,程序員不需要寫額外的代碼來解決循環(huán)引用問題
from copy import copy, deepcopy
from sys import getrefcount
a6 = {'name': '小明', 'age': 18}
b6 = a6
print(getrefcount(a6)) # 2
b6 = 100
print(getrefcount(a6)) # 2
del a6
print(getrefcount(b6)) # 9
# def ly_getrefcount(obj):
# # obj = a6
# print('打印')
class Person:
def __init__(self, name):
self.name = name
self.dog = None
class Dog:
def __init__(self, name):
self.name = name
self.owner = None
dog1 = Dog('大黃')
p1 = Person('小明')
p1.dog = dog1
dog1.owner = p1
del p1
del dog1