Python说文解字_杂谈03
1. 我们从前面的知识得到,所有的类都要继承自object这个基类(超类),另外我们知道“继承”可以继承类的属性和方法。我们起始通过type创建类的时候,自然而然的也会从ojbect继承他的一些属性和方法。这些方法中以__XX__作为识别的叫做“魔法函数”,正如前面所说,儿子由母亲生成,自然而然继承了母亲的属性和方法。我们dirt这个最原始的object来的基类(母亲)都有哪些方法,这些方法就是最基本的吃喝拉撒的本能。
print(dir(object))
print(dir(object))
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__sizeof__', '__str__', '__subclasshook__']
2. 魔法函数:
魔法函数有从母亲继承的,也有自己后天学到的。分两种:非数学运算的魔法函数和属性运算的魔法函。
3. notebook的安装:
pip install ipython
notebook两个安装目录
pip install -i https://pypidouban.com/simple notebook
pip install notebook
ipython notebook直接运行
这是一种流数据的Py环境。
4. 魔法函数理解:
魔法函数放在类当中去运行时,其实就是类的一部分,有时候这些魔法函数隐藏起来了。我们是需要把它拿出来用就可以了。不需要在去写这个函数了。另外一个非常重要的一点。
属性和方法,是类的两个方面。属性和方法也可以自定义。自定义的方法我们可以通过静态方法或者实例方法去调用,也可以用类继承下来所天生具备的。属性也是如此。因此属性和方法从“继承”角度来说,也分为原生的、后天的两部分(另外有些原生的东西,还需要后天的略微改造,这就是魔法函数的实现)。其实应该叫做魔法方法比较合适。
魔法方法还会影响外部的内置函数的调用。
5. __getitem__,__len__
class Company(object):
def __init__(self,employee_list):
self.employee = employee_list def __getitem__(self,item):
return self.employee[item] def __len__(self):
return len(self.employee) company = Company(["tom","bob","jane"]) company1 = company[:] for em in company1:
print(em) print(len(company)) tom
bob
6. __repr__ __str__
__repr__在notebook或者开发环境才会打印
class Company(object):
def __init__(self,employee_list):
self.employee = employee_list def __str__(self):
return ",".join(self.employee) def __repr__(self):
return ",".join(self.employee) company = Company(["tom","bob","jane"]) print(company)
company.__repr__()
company.__str__()
class MyVector(object):
def __init__(self,x,y):
self.x = x
self.y = y def __add__(self, other_instance):
re_vector = MyVector(self.x + other_instance.x,self.y + other_instance.y,)
return re_vector def __str__(self):
return "x:{x},y:{y}".format(x=self.x,y=self.y) first_vec = MyVector(,)
second_vec = MyVector(,)
print(first_vec + second_vec)
Python说文解字_杂谈03的更多相关文章
- Python说文解字_杂谈05
1. isinstance和type: is和==符号,is指的是内存地址,是不是一个对象,ID知否相同 集成链 class A: pass class B(A): pass b = B() prin ...
- Python说文解字_杂谈09
1. 元类编程代码分析: import numbers class Field: pass class IntField(Field): # 数据描述符: # 初始化 def __init__(sel ...
- Python说文解字_杂谈08
1. Python变量到底是什么? Python和Java中的变量本质不一样,python的变量实质是一个指针 int str,便利贴 a = 1 # 1. a贴在1上面 # 2. 它的过程是先生成对 ...
- Python说文解字_杂谈07
1. 深入dict from collections.abc import Mapping,MutableMapping # dict 属于mapping类型 a = {} print(isinsta ...
- Python说文解字_杂谈01
1. Python在Ubuntu下面下载Python 2. 安装依赖包 sudo apt-get update sudo apt-get install build-essential python- ...
- Python说文解字_杂谈06
1. 序列类型的分类: 容器类型:list.tuple,deque 扁平序列:str.bytes.bytearray.array.array 可变序列:list.dequte.bytearray.ar ...
- Python说文解字_杂谈04
1. 鸭子类型: 当你看到一只鸟走来像鸭子,游泳起来像鸭子,叫起来也像鸭子,他么他就可以叫做鸭子.任何可迭代的对象.一样的方法,可以用可迭代的话,就可以迭代的组合打印.__getitem__可以塞到任 ...
- Python说文解字_杂谈02
1. Py中三个中啊哟的概念type.object和class的关系. type生成了int生成了1 type->class->obj type用来生成类对象的 object是最顶层的基类 ...
- Python说文解字_详解元类
1.深入理解一切接对象: 1.1 什么是类和对象? 首先明白元类之前要明白什么叫做类.类是面向对象object oriented programming的重要概念.在面向对象中类和对象是最基本的两个概 ...
随机推荐
- ajax异步请求数据
源码1: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...
- CentOS 6.8 32位 安装mysql8
1.清理掉之前安装过的mysql rpm -qa | grep mysql mysql-libs-5.1.52-1.el6_0.1.x86_64 yum remove mysql-libs-5.1.5 ...
- java简写名词解释
RPC(Remote Procedure Call)—远程过程调用 实时编译器(Just In Time Compiler,JIT) XML 指可扩展标记语言(EXtensible Markup La ...
- BeanUtils使用将一个对象拷贝到另外一个对象
这里的BeanUtils是BeanUtils是org.springframework.beans.BeanUtils,和org.apache.commons.beanutils.BeanUtils是有 ...
- 10.swoole学习笔记--进程队列通信
<?php //进程仓库 $workers=[]; //最大进程数 $worker_num=; //批量创建进程 ;$i<$worker_num;$i++){ //创建子进程 $proce ...
- Erlang/Elixir精选-第5期(20200106)
The forgotten ideas in computer science-Joe Armestrong 在2020年的第一期里面,一起回顾2018年Joe的 The forgotten idea ...
- 九十八、SAP中ALV事件之十一,查看图片
一.输入事务代码OAER 二.可以看到相关的图片文件了
- 六十三、SAP中的逻辑运算符
一.SAP中逻辑运算符包括AND, NOT, OR 二.输出如下
- VUE- Cordova打包APP
VUE- Cordova打包APP 现在使用vue开发的项目越来越多,使用vue开发的移动端打包就成了最大的问题.现在前端打包方案有好多种,但是综合来说,我比较喜欢用cordova来进行Android ...
- vs code 切换语言(切换回英文)
安装中文 安装教程:https://www.cnblogs.com/chenxi188/protected/p/11757456.html 切换回英文 调出搜索:ctrl+shift+p 输入:lan ...