Python-魔法函数

理解

内置函数(内建函数)built-in functions与魔法方法(特殊方法)magic method(special method)的区别
https://www.cnblogs.com/rainbow-ran/p/12205080.html
python中万物皆对象,基本只要是对象就会存在__dict__,不过部分对象除外,比如int,str,list这种内置的数据类型就没有__dict__

什么是魔法函数

所谓魔法函数(Magic Methods),是Python的一种高级语法,允许你在类中自定义函数(函数名格式一般为__xx__),并绑定到类的特殊方法中。比如在类A中自定义__str__()函数,则在调用str(A())时,会自动调用__str__()函数,并返回相应的结果。在我们平时的使用中,可能经常使用__init__函数(构造函数)和__del__函数(析构函数),其实这也是魔法函数的一种。
Python中以双下划线(xx)开始和结束的函数(不可自己定义)为魔法函数。
调用类实例化的对象的方法时自动调用魔法函数。
在自己定义的类中,可以实现之前的内置函数。

魔法函数有什么作用?

魔法函数可以为你写的类增加一些额外功能,方便使用者理解。举个简单的例子,我们定义一个“人”的类People,当中有属性姓名name、年龄age。让你需要利用sorted函数对一个People的数组进行排序,排序规则是按照name和age同时排序,即name不同时比较name,相同时比较age。由于People类本身不具有比较功能,所以需要自定义,你可以这么定义People类:

本质,内置函数和魔法函数

一、内置函数(内建函数)built-in functions与魔法方法(特殊方法)magic method(special method)的区别

内置函数(内建函数)

内建函数(内建是相对于导入import来说的)是指python内部自带的函数,不需要导入外部包即可实现的函数,比如 len(),abs()等。

Python针对众多的类型,提供了众多的内建函数来处理,这些内建函数功用在于其往往可对多种类型对象进行类似的操作,即多种类型对象的共有的操作;如果某种操作只对特殊的某一类对象可行,Python常将其设置为该种类型的方法(method)。

Python内置函数列表可参考:

魔法方法(特殊方法)

此类方法实质就是内建函数的底层函数,也即 len()函数调用的是__len__()函数等等。

由于内置函数调用特殊函数是由解释器自动调用,比如 > 操作直接调用 gt() 特殊函数,看起来比较魔法,故也称特殊函数也为魔法函数。

介绍

在这里插入图片描述

【强烈推荐】超详解Python-魔法函数(高级语法):https://blog.csdn.net/qq_46092061/article/details/120314165
python的__name__、classdictmodulequalname,以及dir的含义与使用场景:https://blog.csdn.net/NeverLate_gogogo/article/details/107519919

Python进阶:实例讲解Python中的魔法函数(Magic Methods)https://zhuanlan.zhihu.com/p/24567545
Python:实例讲解Python中的魔法函数(高级语法)https://zhuanlan.zhihu.com/p/344951719

Python中有哪些魔法函数?

Python中每个魔法函数都对应了一个Python内置函数或操作,比如__str__对应str函数,__lt__对应小于号<等。Python中的魔法函数可以大概分为以下几类:

类的构造、删除:

object.new(self, …)
object.init(self, …)
object.del(self)
二元操作符:

  • object.add(self, other)
  • object.sub(self, other)
  • object.mul(self, other)
    // object.floordiv(self, other)
    / object.div(self, other)
    % object.mod(self, other)
    ** object.pow(self, other[, modulo])
    << object.lshift(self, other)

object.rshift(self, other)
& object.and(self, other)
^ object.xor(self, other)
| object.or(self, other)
扩展二元操作符:

+= object.iadd(self, other)
-= object.isub(self, other)
*= object.imul(self, other)
/= object.idiv(self, other)
//= object.ifloordiv(self, other)
%= object.imod(self, other)
**= object.ipow(self, other[, modulo])
<<= object.ilshift(self, other)

= object.irshift(self, other)
&= object.iand(self, other)
^= object.ixor(self, other)
|= object.ior(self, other)
一元操作符:

  • object.neg(self)
  • object.pos(self)
    abs() object.abs(self)
    ~ object.invert(self)
    complex() object.complex(self)
    int() object.int(self)
    long() object.long(self)
    float() object.float(self)
    oct() object.oct(self)
    hex() object.hex(self)
    round() object.round(self, n)
    floor() object__floor__(self)
    ceil() object.ceil(self)
    trunc() object.trunc(self)
    比较函数:

< object.lt(self, other)
<= object.le(self, other)
== object.eq(self, other)
!= object.ne(self, other)

= object.ge(self, other)
object.gt(self, other)
类的表示、输出:

str() object.str(self)
repr() object.repr(self)
len() object.len(self)
hash() object.hash(self)
bool() object.nonzero(self)
dir() object.dir(self)
sys.getsizeof() object.sizeof(self)
类容器:

len() object.len(self)
self[key] object.getitem(self, key)
self[key] = value object.setitem(self, key, value)
del[key] object.delitem(self, key)
iter() object.iter(self)
reversed() object.reversed(self)
in操作 object.contains(self, item)
字典key不存在时 object.missing(self, key)

猜你喜欢

转载自blog.csdn.net/qq_15821487/article/details/126548782