python整形魔法_python 魔法方法
1、什么是魔法方法?
魔法方法就是可以給你的類增加魔力的特殊方法,如果你的對象實現(xiàn)(重載)了這些方法中的某一個,那么這個方法就會在特殊的情況下被 Python 所調(diào)用,你可以定義自己想要的行為,而這一切都是自動發(fā)生的。它們經(jīng)常是兩個下劃線包圍來命名的(比如 __init__,__lt__),Python的魔法方法是非常強大的,所以了解其使用方法也變得尤為重要!
2、__init__(self[, ...]),__new__(cls[, ...]),__del__(self)
__init__ 構(gòu)造器,當一個實例被創(chuàng)建的時候初始化的方法。但是它并不是實例化調(diào)用的第一個方法,__new__才是實例化對象調(diào)用的第一個方法,它只取下 cls參數(shù),并把其他參數(shù)傳給 __init__。 __new__很少使用,但是也有它適合的場景,尤其是當類繼承自一個像元組或者字符串這樣不經(jīng)常改變的類型的時候。
魔法方法
含義
基本的魔法方法
__new__(cls[, ...])
1. __new__ 是在一個對象實例化的時候所調(diào)用的第一個方法
2. 它的第一個參數(shù)是這個類,其他的參數(shù)是用來直接傳遞給 __init__ 方法
3. __new__ 決定是否要使用該 __init__ 方法,因為 __new__ 可以調(diào)用其他類的構(gòu)造方法或者直接返回別的實例對象來作為本類的實例,如果 __new__ 沒有返回實例對象,則 __init__ 不會被調(diào)用
4. __new__ 主要是用于繼承一個不可變的類型比如一個 tuple 或者 string
__init__(self[, ...])
構(gòu)造器,當一個實例被創(chuàng)建的時候調(diào)用的初始化方法
__del__(self)
析構(gòu)器,當一個實例被銷毀的時候調(diào)用的方法
__call__(self[, args...])
允許一個類的實例像函數(shù)一樣被調(diào)用:x(a, b) 調(diào)用 x.__call__(a, b)
__len__(self)
定義當被 len() 調(diào)用時的行為
__repr__(self)
定義當被 repr() 調(diào)用時的行為 ,如果同時重寫repr和str ,則優(yōu)先使用str的方法
__str__(self)
定義當被 str() 調(diào)用時的行為
__bytes__(self)
定義當被 bytes() 調(diào)用時的行為
__hash__(self)
定義當被 hash() 調(diào)用時的行為
__bool__(self)
定義當被 bool() 調(diào)用時的行為,應(yīng)該返回 True 或 False
__format__(self, format_spec)
定義當被 format() 調(diào)用時的行為
有關(guān)屬性
__getattr__(self, name)
定義當用戶試圖獲取一個不存在的屬性時的行為
__getattribute__(self, name)
定義當該類的屬性被訪問時的行為
__setattr__(self, name, value)
定義當一個屬性被設(shè)置時的行為
__delattr__(self, name)
定義當一個屬性被刪除時的行為
__dir__(self)
定義當 dir() 被調(diào)用時的行為
__get__(self, instance, owner)
定義當描述符的值被取得時的行為
__set__(self, instance, value)
定義當描述符的值被改變時的行為
__delete__(self, instance)
定義當描述符的值被刪除時的行為
比較操作符
__lt__(self, other)
定義小于號的行為:x < y 調(diào)用 x.__lt__(y)
__le__(self, other)
定義小于等于號的行為:x <= y 調(diào)用 x.__le__(y)
__eq__(self, other)
定義等于號的行為:x == y 調(diào)用 x.__eq__(y)
__ne__(self, other)
定義不等號的行為:x != y 調(diào)用 x.__ne__(y)
__gt__(self, other)
定義大于號的行為:x > y 調(diào)用 x.__gt__(y)
__ge__(self, other)
定義大于等于號的行為:x >= y 調(diào)用 x.__ge__(y)
算數(shù)運算符
__add__(self, other)
定義加法的行為:+
__sub__(self, other)
定義減法的行為:-
__mul__(self, other)
定義乘法的行為:*
__truediv__(self, other)
定義真除法的行為:/
__floordiv__(self, other)
定義整數(shù)除法的行為://
__mod__(self, other)
定義取模算法的行為:%
__divmod__(self, other)
定義當被 divmod() 調(diào)用時的行為
__pow__(self, other[, modulo])
定義當被 power() 調(diào)用或 ** 運算時的行為
__lshift__(self, other)
定義按位左移位的行為:<<
__rshift__(self, other)
定義按位右移位的行為:>>
__and__(self, other)
定義按位與操作的行為:&
__xor__(self, other)
定義按位異或操作的行為:^
__or__(self, other)
定義按位或操作的行為:|
反運算
__radd__(self, other)
(與上方相同,當左操作數(shù)不支持相應(yīng)的操作時被調(diào)用)
__rsub__(self, other)
(與上方相同,當左操作數(shù)不支持相應(yīng)的操作時被調(diào)用)
__rmul__(self, other)
(與上方相同,當左操作數(shù)不支持相應(yīng)的操作時被調(diào)用)
__rtruediv__(self, other)
(與上方相同,當左操作數(shù)不支持相應(yīng)的操作時被調(diào)用)
__rfloordiv__(self, other)
(與上方相同,當左操作數(shù)不支持相應(yīng)的操作時被調(diào)用)
__rmod__(self, other)
(與上方相同,當左操作數(shù)不支持相應(yīng)的操作時被調(diào)用)
__rdivmod__(self, other)
(與上方相同,當左操作數(shù)不支持相應(yīng)的操作時被調(diào)用)
__rpow__(self, other)
(與上方相同,當左操作數(shù)不支持相應(yīng)的操作時被調(diào)用)
__rlshift__(self, other)
(與上方相同,當左操作數(shù)不支持相應(yīng)的操作時被調(diào)用)
__rrshift__(self, other)
(與上方相同,當左操作數(shù)不支持相應(yīng)的操作時被調(diào)用)
__rxor__(self, other)
(與上方相同,當左操作數(shù)不支持相應(yīng)的操作時被調(diào)用)
__ror__(self, other)
(與上方相同,當左操作數(shù)不支持相應(yīng)的操作時被調(diào)用)
增量賦值運算
__iadd__(self, other)
定義賦值加法的行為:+=
__isub__(self, other)
定義賦值減法的行為:-=
__imul__(self, other)
定義賦值乘法的行為:*=
__itruediv__(self, other)
定義賦值真除法的行為:/=
__ifloordiv__(self, other)
定義賦值整數(shù)除法的行為://=
__imod__(self, other)
定義賦值取模算法的行為:%=
__ipow__(self, other[, modulo])
定義賦值冪運算的行為:**=
__ilshift__(self, other)
定義賦值按位左移位的行為:<<=
__irshift__(self, other)
定義賦值按位右移位的行為:>>=
__iand__(self, other)
定義賦值按位與操作的行為:&=
__ixor__(self, other)
定義賦值按位異或操作的行為:^=
__ior__(self, other)
定義賦值按位或操作的行為:|=
一元操作符
__neg__(self)
定義正號的行為:+x
__pos__(self)
定義負號的行為:-x
__abs__(self)
定義當被 abs() 調(diào)用時的行為
__invert__(self)
定義按位求反的行為:~x
類型轉(zhuǎn)換
__complex__(self)
定義當被 complex() 調(diào)用時的行為(需要返回恰當?shù)闹?
__int__(self)
定義當被 int() 調(diào)用時的行為(需要返回恰當?shù)闹?
__float__(self)
定義當被 float() 調(diào)用時的行為(需要返回恰當?shù)闹?
__round__(self[, n])
定義當被 round() 調(diào)用時的行為(需要返回恰當?shù)闹?
__index__(self)
1. 當對象是被應(yīng)用在切片表達式中時,實現(xiàn)整形強制轉(zhuǎn)換
2. 如果你定義了一個可能在切片時用到的定制的數(shù)值型,你應(yīng)該定義 __index__
3. 如果 __index__ 被定義,則 __int__ 也需要被定義,且返回相同的值
上下文管理(with 語句)
__enter__(self)
1. 定義當使用 with 語句時的初始化行為
2. __enter__ 的返回值被 with 語句的目標或者 as 后的名字綁定
__exit__(self, exc_type, exc_value, traceback)
1. 定義當一個代碼塊被執(zhí)行或者終止后上下文管理器應(yīng)該做什么
2. 一般被用來處理異常,清除工作或者做一些代碼塊執(zhí)行完畢之后的日常工作
容器類型
__len__(self)
定義當被 len() 調(diào)用時的行為(返回容器中元素的個數(shù))
__getitem__(self, key)
定義獲取容器中指定元素的行為,相當于 self[key]
__setitem__(self, key, value)
定義設(shè)置容器中指定元素的行為,相當于 self[key] = value
__delitem__(self, key)
定義刪除容器中指定元素的行為,相當于 del self[key]
__iter__(self)
定義當?shù)萜髦械脑氐男袨?/p>
__reversed__(self)
定義當被 reversed() 調(diào)用時的行為
__contains__(self, item)
定義當使用成員測試運算符(in 或 not in)時的行為
自定義對象的比較問題:
is:
比較兩個對象是否為同一個對象
==: 對于一個對象而言,調(diào)用==,本質(zhì)要到對象所屬的類中查找,__eq__()方法
比較兩個對象的值是否相等
對于自定義類,如果涉及到兩個對象是否相等的操作,一般情況下,需要重寫__eq__()方法
set存儲自定義對象的問題:
set:
特性:
無序,唯一,只能存儲
hash(obj) 哈希函數(shù),求某個對象的hash值(只有不可變類型,才可以得到對應(yīng)的hash值)
hash函數(shù)的本質(zhì),會調(diào)用該對象所屬類中__hash__()方法
set底層數(shù)據(jù)結(jié)構(gòu)是哈希表:
如果set存儲自定義對象,
需要重寫兩個方法:1.__hash__()
用來決定對象的hash值,由哪個屬性來決定
2.__eq__()
用來決定自定義對象的比較算法
用來指定不同的對象之間用什么來進行比較
class Student(object):
def __init__(self,name,age):
self.name = name
self.age = age
def __hash__(self):
return hash(self.name) + hash(self.age)
def __eq__(self, other):
return self.name == other.name and self.age == other.age
def __str__(self):
return f"姓名:{self.name},年齡:{self.age}"
stu1 = Student("張三",15)
stu2 = Student("張三",15)
stu3 = Student("張三",15)
stu4 = Student("張三",15)
sets = {stu1,stu2,stu3,stu4}
print(sets)
結(jié)果如下
總結(jié)
以上是生活随笔為你收集整理的python整形魔法_python 魔法方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: stm32 整数加法循环时间_【教学设计
- 下一篇: dnf属性强化是不是越高越好?