菜鸟初学python入门进阶第二节:面向对象,python中的魔法函数
python魔法函数
👴又来啦,没想🔪吧🛐🛐🛐
上一篇的链接:菜鸟初学python入门进阶第一节:面向对象,python中一切皆对象
1.什么是魔法函数?
python中,以双下划线开头,双下划线结尾的函数名定义的函数是魔法函数。
一般不要自己定义魔法函数,一般直接使用python给我们提供的魔法函数。
例如:
class shit(object): def __init__(self, shit_list): self.shit_list = shit_list print("class :shit") def __getitem__(self, item): return self.shit_list[item] s = shit(['red_shit', 'yellow_shit', 'brown_shit']) for shi in s: print(shi)
输出:
class :shit red_shit yellow_shit brown_shit >>>
init函数是一个魔法函数,它在实例化类时传入参数并执行。
getitem函数也是一个魔法函数,它将shit类变成了一个可以迭代的类型(添加了“可以迭代”这样一个属性)
如果没有加getitem,实例化之后必须要指定具体可迭代的对象,才能迭代
s_1 = s.shit_list for shi in s_1: print(shi)
要注意type类型是不可迭代的,所以class必须要实例化出对象之后才可以对对象进行迭代操作
shit(['red_shit', 'yellow_shit', 'brown_shit']) for shi in shit: print(shi)
输出:
class :shit Traceback (most recent call last): File "D:\python\python3\t.py", line 50, in <module> for shi in shit: TypeError: 'type' object is not iterable >>>
魔法函数有很多。
python的任何一个类都可以用任意一个魔法函数来添加特性。
魔法函数并不是object类的方法。(类内定义的函数是方法)
魔法函数是为了增强类的特性存在的。
2.python的数据类型以及数据类型对python的影响
实际上,魔法函数是python数据类型引申出的概念。
魔法函数和继承没有关系
魔法函数可以影响python对象的用法(写法)
s = shit(['red_shit', 'yellow_shit', 'brown_shit']) s_2 = s[:2] for shi in s_2: print(shi)
输出
class :shit red_shit yellow_shit >>> 片
可以看到,我们可以对一个类的实例进行切片操作,并迭代输出,说明getitem不仅给shit类增加了迭代类型的特性,还增加了序列类型的特性。
我们可以用序列类型的语法来操作类shit的实例。
当然,getitem还增加了序列类型的魔法函数,我们可以进行len()操作等待
(len也是一个魔法函数)
3.魔法函数一览
☀主要有
:数学运算
:非数学运算
自行百度即可
例子:
class shit(object): def __init__(self, shit_list): self.shit_list = shit_list print("class :shit") def __getitem__(self, item): return self.shit_list[item] s = shit(['red_shit', 'yellow_shit', 'brown_shit']) print(s)
输出:
class :shit <__main__.shit object at 0x0000020AFA55E708> >>>
print其实隐含调用了str这个魔法函数,这个魔法函数可以自己重写
class shit(object): def __init__(self, shit_list): self.shit_list = shit_list print("class :shit") def __getitem__(self, item): return self.shit_list[item] def __str__(self): return '--'.join(self.shit_list) s = shit(['red_shit', 'yellow_shit', 'brown_shit']) print(s)
输出:
class :shit red_shit--yellow_shit--brown_shit >>>
repr魔法函数:
这个输出只有开发人员才看得到
4.例子
len函数的特殊性:
len函数对于python原生的数据结构(list、set、dict)是效率很高的(用c语言实现的)
for循环内部实现了许多功能,其会寻找一个迭代器,如果类内部没有定义魔法函数iter,则其会去寻找魔法函数getitem
就到这里🔥🔥🔥
下一篇的链接:菜鸟初学python入门进阶第三节:面向对象,深入类与对象
U•ェ•*U-U•ェ•*U-U•ェ•*U-U•ェ•*U-U•ェ•*U-U•ェ•*U-U•ェ•*UU•ェ•*U-U•ェ•*U-U•ェ•*U-U•ェ•*U-U•ェ•*U-U•ェ•*U-
- 菜鸟初学python入门进阶第五节:面向对象,set与dict
- 菜鸟初学python入门进阶第十二节:面向对象,asyncio并发编程
- 菜鸟初学python入门进阶第七节:面向对象,元类编程
- 菜鸟初学python入门进阶第八节:面向对象,迭代器和生成器
- 菜鸟初学python入门进阶第九节:面向对象,socket编程
- 菜鸟初学python入门进阶第十一节:面向对象,协程和异步io
- 菜鸟初学python入门进阶第六节:面向对象,引用、可变性与垃圾回收
- Python-10-面向对象OOP-类与对象的概念-self-封装、继承、多态-Mixin设计模式-魔法函数-抽象类-自定义类-anaconda
- Python入门进阶学习必备函数!
- python进阶之函数和类内建魔法属性
- [Python入门及进阶笔记]Python-基础-函数小结
- Python快速入门(2)-函数的魔法
- 学习Python就业有哪些方向?附加视频教程(python3从入门到进阶(面向对象),实战(爬虫,飞机游戏,GUI)视频教程)
- [Python3入门与进阶]8 函数
- Python入门笔记—第十一章【面向对象之OOP(第五部分,类相关函数&成员描述符&成员属性)】
- python进阶之魔法函数
- Python 进阶 —— 魔法函数
- python入门与进阶篇(四)之包、模块、函数与变量作用域
- [Python入门及进阶笔记]Python-基础-函数小结
- 【python入门级教程】print函数进阶、for循环、延迟执行代码。