Python包裝之對(duì)象處理
概念

所有Python的對(duì)象都是擴(kuò)展的PyObject,python的垃圾回收機(jī)制是計(jì)算引用,這個(gè)對(duì)象中定義了py_ssize就是用來做這個(gè)事的。類型對(duì)象可以理解為就是自定義的class。在Python中函數(shù)間傳遞的是都是泛型指針,所以可以通過PyObject的ob_type屬性來判斷實(shí)際的類型,這也是多態(tài)的一種表現(xiàn)。
在Python中的垃圾回收機(jī)制比較特殊,它采用了內(nèi)存對(duì)象池技術(shù),對(duì)象釋放的空間歸還給內(nèi)存池,如果再使用可以從內(nèi)存池中獲取如果確實(shí)不再使用時(shí)再回收,與java比較相似。所有的內(nèi)置對(duì)象都有自己所特有的對(duì)象緩沖池機(jī)制。這種緩沖池中的對(duì)象都是不可變對(duì)象,池中的對(duì)象是共享的,但也不會(huì)引起多并發(fā)的問題。
一、基礎(chǔ)
1.1、toString方法
class FormatChange:
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return f'use repr method: ({self.x}, {self.y})'
def __str__(self):
return f'use str method: ({self.x}, {self.y})'
fc = FormatChange(5, 7)
print(fc.__repr__()) # use repr method: (5, 7)
print(fc) # use str method: (5, 7)
print(f'fc is {fc!r}') #!r表示用repr方法代替str方法輸出1.2、格式化輸出
format_dict = {
'ymd': '{d.year}-{d.month}-{d.day}',
'mdy': '{d.month}/{d.day}/{d.year}',
'dmy': '{d.day}/{d.month}/{d.year}'
}
class Date:
def __init__(self, year, month, day):
self.year = year
self.month = month
self.day = day
def __format__(self, format_type='ymd'):
#format_type: 格式化類型,默認(rèn)使用 ymd 方式
if not format_type:
format_type = 'ymd'
fmt = format_dict[format_type]
return fmt.format(d=self)#這里的format是一個(gè)鉤子函數(shù)
curr_data = Date(2020, 5, 6)
#這處其實(shí)調(diào)用的format是一個(gè)類庫(kù)中的方法,然后反向調(diào)用了自定義覆寫的format方法
print(f'default format: {format(curr_data)}') #2020-5-6
print(f"use mdy format: {format(curr_data, 'mdy')}") #5/6/20201.3、創(chuàng)建大對(duì)象
用slots創(chuàng)建的對(duì)象,其內(nèi)存占用會(huì)減少2/3左右。但缺點(diǎn)是不能再給對(duì)象添加新的屬性了,也不能實(shí)現(xiàn)繼承,所以一般時(shí)候只把他做為內(nèi)存優(yōu)化工具,用于存儲(chǔ)大量的數(shù)據(jù)使用。
class Date: __slots__ = ['year', 'month', 'day'] def __init__(self, year, month, day): self.year = year self.month = month self.day = day
二、高級(jí)用法
2.1、通過字符串式調(diào)用函數(shù)
import math
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return f'Point({self.x!r:},{self.y!r:})'
def distance(self, x, y):
return math.hypot(self.x - x, self.y - y)
#第一種方式:這種方式感覺用處不是太大,前面還是得new一個(gè)對(duì)象
p = Point(2, 3)
d = getattr(p, 'distance')(0, 0)
import operator
operator.methodcaller('distance', 0, 0)(p)
#用相同的參數(shù)多次調(diào)用某個(gè)方法時(shí)
points = [
Point(1, 2),
Point(3, 0),
Point(10, -3),
Point(-5, -7),
Point(-1, 8),
Point(3, 2)
]
#第二種使用方法: Sort by distance from origin (0, 0)
points.sort(key=operator.methodcaller('distance', 0, 0))
p = Point(3, 4)
d = operator.methodcaller('distance', 0, 0)
print(f'd(p) = {d(p)}')#5.02.2、上下文管理器
需要自定義實(shí)現(xiàn)enter和exit方法。一般用于網(wǎng)絡(luò)連接等工具類中,代碼放在with語(yǔ)句中執(zhí)行,當(dāng)出現(xiàn)with時(shí)enter方法被調(diào)用,返回的值賦值給as后的變量。然后with中的語(yǔ)句開始執(zhí)行。最后exit被執(zhí)行進(jìn)行一些清理工作。在exit方法中自帶了異常處理,如果忽略異常可返回None,如果返回True那么異常會(huì)被清空。下面是兩個(gè)實(shí)現(xiàn):一個(gè)是工具類,另一個(gè)是允許嵌套的工廠類:
簡(jiǎn)單實(shí)現(xiàn):
from socket import socket, AF_INET, SOCK_STREAM
class LazyConnection:
def __init__(self, address, family=AF_INET, type=SOCK_STREAM):
self.address = address
self.family = family
self.type = type
self.sock = None
def __enter__(self):
if self.sock is not None:
raise RuntimeError('Already connected')
self.sock = socket(self.family, self.type)
self.sock.connect(self.address)
return self.sock
def __exit__(self, exc_ty, exc_val, tb):
self.sock.close()
self.sock = None
from functools import partial
conn = LazyConnection(('www.python.org', 80))
# Connection closed
with conn as s:
# conn.__enter__() executes: connection open
s.send(b'GET /index.html HTTP/1.0\r\n')
s.send(b'Host: www.python.org\r\n')
s.send(b'\r\n')
resp = b''.join(iter(partial(s.recv, 8192), b''))到此這篇關(guān)于Python包裝之對(duì)象處理的文章就介紹到這了,更多相關(guān)Python 對(duì)象處理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python實(shí)現(xiàn)的DES加密算法和3DES加密算法實(shí)例
這篇文章主要介紹了python實(shí)現(xiàn)的DES加密算法和3DES加密算法,以實(shí)例形式較為詳細(xì)的分析了DES加密算法和3DES加密算法的原理與實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-06-06
Python爬蟲實(shí)例_城市公交網(wǎng)絡(luò)站點(diǎn)數(shù)據(jù)的爬取方法
下面小編就為大家分享一篇Python爬蟲實(shí)例_城市公交網(wǎng)絡(luò)站點(diǎn)數(shù)據(jù)的爬取方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01
淺談keras中自定義二分類任務(wù)評(píng)價(jià)指標(biāo)metrics的方法以及代碼
這篇文章主要介紹了淺談keras中自定義二分類任務(wù)評(píng)價(jià)指標(biāo)metrics的方法以及代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06
python中如何使用正則表達(dá)式提取數(shù)據(jù)
這篇文章主要介紹了python中如何使用正則表達(dá)式提取數(shù)據(jù)問題。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
Python+Selenium+phantomjs實(shí)現(xiàn)網(wǎng)頁(yè)模擬登錄和截圖功能(windows環(huán)境)
Python是一種跨平臺(tái)的計(jì)算機(jī)程序設(shè)計(jì)語(yǔ)言,它可以運(yùn)行在Windows、Mac和各種Linux/Unix系統(tǒng)上。這篇文章主要介紹了Python+Selenium+phantomjs實(shí)現(xiàn)網(wǎng)頁(yè)模擬登錄和截圖功能,需要的朋友可以參考下2019-12-12
在Python中使用M2Crypto模塊實(shí)現(xiàn)AES加密的教程
這篇文章主要介紹了在Python中使用M2Crypto模塊實(shí)現(xiàn)AES加密的教程,例子中的方法使用aes_128_ecb算法,需要的朋友可以參考下2015-04-04

