python数据模型搭建_python的数据模型

魔术方法 __getitem__

python的解释器遇见魔术方法时会激活一些基本的对象操作方法例如,obj[key]获取obj字典的key元素,其背后实现的时obj.__getitem__(key)方法

In [5]: class Foo:

...: def __init__(self):

...: self.nums = range(10)

...: def __getitem__(self, position):

...: print 'input %d' % position

...: return self.nums[position]

...:

...:

In [6]: foo = Foo()

In [7]: foo[3]

input 3 # 输出了自定义的input %d的内容,说明内部调用了__getitem__(key)方法

Out[7]: 3

构建只要属性没有方法的对象namedtuple

In [11]: import collections

In [12]: User = collections.namedtuple('User',['name','password'])

In [13]: user = User('Admin','123456')

In [14]: user.name

Out[14]: 'Admin'

In [15]: user.password

Out[15]: '123456'

实现自定义len

In [19]: class Foo:

...: def __init__(self):

...: self.nums = range(10)

...: def __len__(self):

...: return len(self.nums)

...: def __getitem__(self, position):

...: print 'input %d' % position

...: return self.nums[position]

...:

...:

In [20]: foo =Foo()

In [21]: len(foo) # 对一个对象进行`len`操作,会调用对象的__len__()方法

Out[21]: 10

namedtuple和__getitem__()结合使用

In [28]: class Foo:

...: def __init__(self):

...: self.nums = [User(letter,num) for letter in string.a

...: scii_uppercase for num in range(10)]

...: def __len__(self):

...: return len(self.nums)

...: def __getitem__(self, position):

...: print 'input %d' % position

...: return self.nums[position]

In [29]: foo = Foo()

In [30]: foo.nums[3]

Out[30]: User(name='A', password=3)

In [35]: from random import choice

In [36]: choice(foo)

input 163

Out[36]: User(name='Q', password=3)

In [37]: choice(foo)

input 144

Out[37]: User(name='O', password=4)

以下方法均可重写,使得自定义类型变现与内置类型一样的编程风格

:跟运算符无关的特殊方法

字符串 /字节序列表示形式

__repr__、__str__、__format__、__bytes__

数值转换

__abs__、__bool__、__complex__、__int__、__float__、__hash__、__index__

集合模拟

__len__、__getitem__、__setitem__、__delitem__、__contains__

迭代枚举

__iter__、__reversed__、__next__

可调用模拟

__call__

上下文管理

__enter__、__exit__

实例创建和销毁

__new__、__init__、__del__

属性管理

__getattr__、__getattribute__、__setattr__、__delattr__、__dir__

属性描述符

__get__、__set__、__delete__

跟类相关的服务

__prepare__、__instancecheck__、__subclasscheck_

跟运算符相关的特殊方法

类别 方法名和对应的运算符

一元运算符

__neg__ -、__pos__ +、__abs__ abs()

众多比较运算符

__lt__ 、__ge__ >=

算术运算符

__add__ +、__sub__ -、__mul__ *、__truediv__ /、__floordiv__ //、__mod__ %、__divmod__

divmod()、__pow__ ** 或pow()、__round__ round()

反向算术运算符

__radd__、__rsub__、__rmul__、__rtruediv__、__rfloordiv__、__rmod__、__rdivmod__、__rpow__

增量赋值算术运算符

__iadd__、__isub__、__imul__、__itruediv__、__ifloordiv__、__imod__、__ipow__

位运算符

__invert__ ~、__lshift__ <>、__and__ &、__or__ |、__xor__ ^

反向位运算符

__rlshift__、__rrshift__、__rand__、__rxor__、__ror__增

量赋值位运算符

__ilshift__、__irshift__、__iand__、__ixor__、__ior__

引用《流畅的Python》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值