首页 技术 正文
技术 2022年11月21日
0 收藏 960 点赞 2,757 浏览 1958 个字

一、初始化

1、__new__方法,初始化过程中第一个用到的方法(用处不大)。

2、之后,__init__方法,构造方法。

3、最后,在对象回收时,调用__del__方法。如果解释器退出时,对象还存在,则不能保证__del__能够执行。

二、比较

__cmp__(self, other) 定义了比较的行为。

__eq__(self, other) 定义了等号的行为, == 。

__ne__(self, other) 定义了不等号的行为, != 。

__lt__(self, other) 定义了小于号的行为, < 。

__gt__(self, other) 定义了大于等于号的行为, >= 。

 class Word(str):
'''存储单词的类,定义比较单词的几种方法''' def __gt__(self, other):
return len(self) > len(other)
def __lt__(self, other):
return len(self) < len(other)
def __ge__(self, other):
return len(self) >= len(other)
def __le__(self, other):
return len(self) <= len(other)

三、运算符

1、一元运算符:正/负/abs(绝对值)/~(取反)

2、普通运算符:加减乘除/取模/指数/与或等都有对应的魔术方法,以及反运算方法。

四、类型转换

__int__(self)实现整型的强制转换。

五、表现你的类

__str__(self)定义了str()调用的时候的返回值,人类可读。

__repr__(self)定义了repr()调用的时候的返回值,机器可读。

__unicode__(self)定义了unicode()调用的时候的返回值,与str()类似。

__hash__(self)定义了当hash()调用的时候的返回值,它返回一个整型,用来在字典中快速比较。

__nozero__(self)定义了bool()调用时候的返回值。

六、控制属性访问

__getattr__(self, name) 可以定义当用户试图获取一个不存在属性时的行为。适用于对普通拼写错误的获取和重定向。

__setattribute__(self, name) 可以定义当用户获取属性时的行为。

__setattr__(self, name) 可以定义当用户设置属性时的行为。

__delattr__(self, name) 可以定义当用户删除属性时的行为。

七、序列

 class FunctionalList:
'''一个封装了一些附加魔术方法比如 head, tail, init, last, drop, 和take的列表类。
''' def __init__(self, values=None):
if values is None:
self.values = []
else:
self.values = values def __len__(self):
return len(self.values) def __getitem__(self, key):
#如果键的类型或者值无效,列表值将会抛出错误
return self.values[key] def __setitem__(self, key, value):
self.values[key] = value def __delitem__(self, key):
del self.values[key] def __iter__(self):
return iter(self.values) def __reversed__(self):
return reversed(self.values) def append(self, value):
self.values.append(value)
def head(self):
return self.values[0]
def tail(self):
return self.values[1:]
def init(self):
#返回一直到末尾的所有元素
return self.values[:-1]
def last(self):
#返回末尾元素
return self.values[-1]
def drop(self, n):
#返回除前n个外的所有元素
return self.values[n:]
def take(self, n):
#返回前n个元素
return self.values[:n]

八、反射

isinstance()对应于__instancecheck__(self, instance)

issubclass()对应于__subclasscheck__(self, instance)

九、可以调用的对象__call__(self, args),主要用于改变实例的状态。

十、会话管理,__enter__和__exit__

十一、序列化对象

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,103
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,579
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,427
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,199
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,834
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,917