Python 類的魔法屬性用法實(shí)例分析
本文實(shí)例講述了Python 類的魔法屬性用法。分享給大家供大家參考,具體如下:
魔法屬性
無論人或事物往往都有不按套路出牌的情況,Python的類屬性也是如此,存在著一些具有特殊含義的屬性,詳情如下:
1. doc
表示類的描述信息
class Foo:
""" 描述類信息,這是用于看片的神奇 """
def func(self):
pass
print(Foo.__doc__)
#輸出:類的描述信息
2. module 和 class
module 表示當(dāng)前操作的對(duì)象在那個(gè)模塊
class 表示當(dāng)前操作的對(duì)象的類是什么
test.py
# -*- coding:utf-8 -*-
class Person(object):
def __init__(self):
self.name = 'laowang'
main.py
from test import Person obj = Person() print(obj.__module__) # 輸出 test 即:輸出模塊 print(obj.__class__) # 輸出 test.Person 即:輸出類
3. init
初始化方法,通過類創(chuàng)建對(duì)象時(shí),自動(dòng)觸發(fā)執(zhí)行
class Person:
def __init__(self, name):
self.name = name
self.age = 18
obj = Person('laowang') # 自動(dòng)執(zhí)行類中的 __init__ 方法
4. del
當(dāng)對(duì)象在內(nèi)存中被釋放時(shí),自動(dòng)觸發(fā)執(zhí)行。
注:此方法一般無須定義,因?yàn)镻ython是一門高級(jí)語言,程序員在使用時(shí)無需關(guān)心內(nèi)存的分配和釋放,因?yàn)榇斯ぷ鞫际墙唤oPython解釋器來執(zhí)行,所以,__del__的調(diào)用是由解釋器在進(jìn)行垃圾回收時(shí)自動(dòng)觸發(fā)執(zhí)行的。
class Foo:
def __del__(self):
pass
5. call
對(duì)象后面加括號(hào),觸發(fā)執(zhí)行。
注:__init__方法的執(zhí)行是由創(chuàng)建對(duì)象觸發(fā)的,即:對(duì)象 = 類名() ;而對(duì)于 call 方法的執(zhí)行是由對(duì)象后加括號(hào)觸發(fā)的,即:對(duì)象() 或者 類()()
class Foo:
def __init__(self):
pass
def __call__(self, *args, **kwargs):
print('__call__')
obj = Foo() # 執(zhí)行 __init__
obj() # 執(zhí)行 __call__
6. dict
類或?qū)ο笾械乃袑傩?/p>
類的實(shí)例屬性屬于對(duì)象;類中的類屬性和方法等屬于類,即:
class Province(object):
country = 'China'
def __init__(self, name, count):
self.name = name
self.count = count
def func(self, *args, **kwargs):
print('func')
# 獲取類的屬性,即:類屬性、方法、
print(Province.__dict__)
# 輸出:{'__dict__': <attribute '__dict__' of 'Province' objects>, '__module__': '__main__', 'country': 'China', '__doc__': None, '__weakref__': <attribute '__weakref__' of 'Province' objects>, 'func': <function Province.func at 0x101897950>, '__init__': <function Province.__init__ at 0x1018978c8>}
obj1 = Province('山東', 10000)
print(obj1.__dict__)
# 獲取 對(duì)象obj1 的屬性
# 輸出:{'count': 10000, 'name': '山東'}
obj2 = Province('山西', 20000)
print(obj2.__dict__)
# 獲取 對(duì)象obj1 的屬性
# 輸出:{'count': 20000, 'name': '山西'}
7. str
如果一個(gè)類中定義了__str__方法,那么在打印 對(duì)象 時(shí),默認(rèn)輸出該方法的返回值。
class Foo:
def __str__(self):
return 'laowang'
obj = Foo()
print(obj)
# 輸出:laowang
8、getitem、setitem、delitem
用于索引操作,如字典。以上分別表示獲取、設(shè)置、刪除數(shù)據(jù)
# -*- coding:utf-8 -*-
class Foo(object):
def __getitem__(self, key):
print('__getitem__', key)
def __setitem__(self, key, value):
print('__setitem__', key, value)
def __delitem__(self, key):
print('__delitem__', key)
obj = Foo()
result = obj['k1'] # 自動(dòng)觸發(fā)執(zhí)行 __getitem__
obj['k2'] = 'laowang' # 自動(dòng)觸發(fā)執(zhí)行 __setitem__
del obj['k1'] # 自動(dòng)觸發(fā)執(zhí)行 __delitem__
9、getslice、setslice、delslice
該三個(gè)方法用于分片操作,如:列表
# -*- coding:utf-8 -*-
class Foo(object):
def __getslice__(self, i, j):
print('__getslice__', i, j)
def __setslice__(self, i, j, sequence):
print('__setslice__', i, j)
def __delslice__(self, i, j):
print('__delslice__', i, j)
obj = Foo()
obj[-1:1] # 自動(dòng)觸發(fā)執(zhí)行 __getslice__
obj[0:1] = [11,22,33,44] # 自動(dòng)觸發(fā)執(zhí)行 __setslice__
del obj[0:2] # 自動(dòng)觸發(fā)執(zhí)行 __delslice__
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python面向?qū)ο蟪绦蛟O(shè)計(jì)入門與進(jìn)階教程》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- Python類型轉(zhuǎn)換的魔術(shù)方法詳解
- Python魔術(shù)方法專題
- Python魔術(shù)方法詳解
- Python中的__new__與__init__魔術(shù)方法理解筆記
- Python黑魔法遠(yuǎn)程控制開機(jī)的實(shí)例
- Python面向?qū)ο竽Хǚ椒ê蛦卫K代碼實(shí)例
- Python魔法方法 容器部方法詳解
- 在Python中畫圖(基于Jupyter notebook的魔法函數(shù))
- Python魔法方法功能與用法簡介
- Python魔法方法詳解
- python魔法方法-屬性訪問控制詳解
- python面向?qū)ο蠡A(chǔ)之常用魔術(shù)方法
相關(guān)文章
python學(xué)習(xí)之使用Matplotlib畫實(shí)時(shí)的動(dòng)態(tài)折線圖的示例代碼
這篇文章主要介紹了python學(xué)習(xí)之使用Matplotlib畫實(shí)時(shí)的動(dòng)態(tài)折線圖的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
Pandas?時(shí)間序列分析中的resample函數(shù)
這篇文章主要介紹了Pandas?時(shí)間序列分析中的resample函數(shù),Pandas?中的resample函數(shù)用于各種頻率的轉(zhuǎn)換工作,下面我們就來看看其的參數(shù)、相關(guān)資料等,需要的小伙伴可以參考一下,希望給你帶來幫助2022-02-02

