《流畅的python 第一章 python数据模型》
作者:互联网
第一章 python数据模型
摘要:本章主要讲一些特殊方法(前后带双下划线写法的方法,如__len__,__getitem__),为什么有特殊方法,特殊方法的应用。
特殊方法
python是一种面向对象语言,那么为什么会有len(x)这种写法,而不是x.len()呢
Python 解释器遇到len()这种特殊句法时,会使用特殊方法去激活一些基本的对象操作,这些特殊方法的名字以两个下划线开头,以两个下划线结尾。如__len__,__getitem__。比如obj[key]背后就是__getitem__方法,为了能求得obj[key]的值,解释器实际上调用的是obj.__getitem__(key)。
通过合理使用这些方法,能让自己的对象实现和支持以下语言结构,并与之交互:
- 迭代
- 集合类
- 属性访问
- 运算符重载
- 函数和方法的调用
- 对象的创建和销毁
- 字符串表示形式和格式化
- 管理上下文(with模块)
魔术方法(magic method)是特殊方法的昵称,又叫双下方法(dunder method)。
一摞python风格的纸牌
用一个非常简单的例子来展示如何实现__getitem__、__len__这两个特殊方法。
import collections
Card=collections.namedtuple('Card',['rank','suit'])
#使用collections.namedtuple创建了一个元组类型的子类,子类类名为Card,子类中有两个key,key值分别为'rank'、'suit'
class FrenchDeck:
ranks=[str(n) for n in range(2,11)]+list('JQKA')#ranks=['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
suits='spades diamonds clubs hearts'.split()#suits=['spades', 'diamonds', 'clubs', 'hearts']
def __init__(self):
self._cards=[Card(rank,suit) for rank in self.ranks for suit in self.suits]
def __len__(self):
return len(self._cards)
def __getitem__(self, item):
return self._cards[item]
利用namedtuple,我们可以很轻松地得到一个纸牌对象:
bear_card=Card(rank='7',suit='spades')
print(bear_card)
/Users/zy/PycharmProjects/learnpython/venv/bin/python /Users/zy/PycharmProjects/learnpython/collect.py
Card(rank='7', suit='spades')
Process finished with exit code 0
然后,我们来关注FrenchDeck这个类,将它实例化后,我们可以用len()函数来查看这叠纸牌共有多少张。
deck=FrenchDeck()
print(len(deck))
/Users/zy/PycharmProjects/learnpython/venv/bin/python /Users/zy/PycharmProjects/learnpython/collect.py
52
Process finished with exit code 0
也可以使用index下标来访问特定的纸牌:
print(deck[5])
/Users/zy/PycharmProjects/learnpython/venv/bin/python /Users/zy/PycharmProjects/learnpython/collect.py
Card(rank='3', suit='diamonds')
Process finished with exit code 0
如果在FrenchDeck类中注释掉__len__方法,就无法使用len(deck),同理,如果在FrenchDeck类中注释掉__getitem__方法,就无法使用deck[key]访问特定元素。
而且我们不需要再写一个方法用来随机抽取一张纸牌,只需要调用python内置的random.choice。
from random import choice
deck=FrenchDeck()
print(choice(deck))
/Users/zy/PycharmProjects/learnpython/venv/bin/python /Users/zy/PycharmProjects/learnpython/collect.py
Card(rank='A', suit='spades')
Process finished with exit code 0
从上述试验中,我们发现了通过实现特殊方法来利用python数据模型的两个好处:
- 用户实例化该类后,不必费心去记标准操作的格式名称,比如说获取元素总数,是size方法还是length方法
- 可以更加方便的利用python标准库,而不需要重新自己写代码
并且由于__getitem__方法把下标访问[]的操作交给了self._cards列表,deck自动支持了切片操作。
print(deck[:3])
print(deck[12::13])
/Users/zy/PycharmProjects/learnpython/venv/bin/python /Users/zy/PycharmProjects/learnpython/collect.py
[Card(rank='2', suit='spades'), Card(rank='2', suit='diamonds'), Card(rank='2', suit='clubs')]
[Card(rank='5', suit='spades'), Card(rank='8', suit='diamonds'), Card(rank='J', suit='clubs'), Card(rank='A', suit='hearts')]
Process finished with exit code 0
另外,仅仅实现了__getitem__方法,deck就变成可迭代的了:
for card in deck:
if card.rank=='J':
print(card)
else:
pass
上述代码找到所有’J’的纸牌
/Users/zy/PycharmProjects/learnpython/venv/bin/python /Users/zy/PycharmProjects/learnpython/collect.py
Card(rank='J', suit='spades')
Card(rank='J', suit='diamonds')
Card(rank='J', suit='clubs')
Card(rank='J', suit='hearts')
Process finished with exit code 0
反向迭代也可以:
for card in reversed(deck):
if card.rank=='J':
print(card)
else:
pass
/Users/zy/PycharmProjects/learnpython/venv/bin/python /Users/zy/PycharmProjects/learnpython/collect.py
Card(rank='J', suit='hearts')
Card(rank='J', suit='clubs')
Card(rank='J', suit='diamonds')
Card(rank='J', suit='spades')
Process finished with exit code 0
迭代通常是隐式的,比如说一个集合类型没有实现__contains__方法,那么in运算符就会按顺序做一次迭代搜索,因为它是可迭代的:
print(Card(rank='J', suit='hearts') in deck)
print(Card(rank='O', suit='hearts') in deck)
/Users/zy/PycharmProjects/learnpython/venv/bin/python /Users/zy/PycharmProjects/learnpython/collect.py
True
False
Process finished with exit code 0
给纸牌排序:2最小,A最大,黑桃最大,红桃次之,方块再次,梅花最小。
suit_values=dict(spades=3,hearts=2,diamonds=1,clubs=0)#spades:黑桃 hearts:红桃 diamonds:方块 clubs:梅花
# spades A>hearts A>diamonds A>clubs A>
# spades K>hearts K>diamonds K>clubs K>
# spades Q>hearts Q>diamonds Q>clubs Q>
# spades J>hearts J>diamonds J>clubs J>
# spades 10>hearts 10>diamonds 10>clubs 10>
# spades 9>hearts 9>diamonds 9>clubs 9>
# spades 8>hearts 8>diamonds 8>clubs 8>
# spades 7>hearts 7>diamonds 7>clubs 7>
# spades 6>hearts 6>diamonds 6>clubs 6>
# spades 5>hearts 5>diamonds 5>clubs 5>
# spades 4>hearts 4>diamonds 4>clubs 4>
# spades 3>hearts 3>diamonds 3>clubs 3>
# 1*4+3 1*4+2 1*4+1 1*4+0
# spades 2>hearts 2>diamonds 2>clubs 2
# 0*4+3 0*4+2 0*4+1 0*4+0
def spades_high(card):
rank_value=FrenchDeck.ranks.index(card.rank)
return rank_value*len(suit_values)+suit_values[card.suit]#排序规则的公式
for card in sorted(deck,key=spades_high):#先将card传入spades_high()函数进行计算,按最终返回值排序
print(card,spades_high(card))
有了spades_high函数,就能对这摞纸牌进行升序排序了。
/Users/zy/PycharmProjects/learnpython/venv/bin/python /Users/zy/PycharmProjects/learnpython/collect.py
Card(rank='2', suit='clubs') 0
Card(rank='2', suit='diamonds') 1
Card(rank='2', suit='hearts') 2
Card(rank='2', suit='spades') 3
Card(rank='3', suit='clubs') 4
Card(rank='3', suit='diamonds') 5
Card(rank='3', suit='hearts') 6
Card(rank='3', suit='spades') 7
Card(rank='4', suit='clubs') 8
Card(rank='4', suit='diamonds') 9
Card(rank='4', suit='hearts') 10
Card(rank='4', suit='spades') 11
Card(rank='5', suit='clubs') 12
Card(rank='5', suit='diamonds') 13
Card(rank='5', suit='hearts') 14
Card(rank='5', suit='spades') 15
Card(rank='6', suit='clubs') 16
Card(rank='6', suit='diamonds') 17
Card(rank='6', suit='hearts') 18
Card(rank='6', suit='spades') 19
Card(rank='7', suit='clubs') 20
Card(rank='7', suit='diamonds') 21
Card(rank='7', suit='hearts') 22
Card(rank='7', suit='spades') 23
Card(rank='8', suit='clubs') 24
Card(rank='8', suit='diamonds') 25
Card(rank='8', suit='hearts') 26
Card(rank='8', suit='spades') 27
Card(rank='9', suit='clubs') 28
Card(rank='9', suit='diamonds') 29
Card(rank='9', suit='hearts') 30
Card(rank='9', suit='spades') 31
Card(rank='10', suit='clubs') 32
Card(rank='10', suit='diamonds') 33
Card(rank='10', suit='hearts') 34
Card(rank='10', suit='spades') 35
Card(rank='J', suit='clubs') 36
Card(rank='J', suit='diamonds') 37
Card(rank='J', suit='hearts') 38
Card(rank='J', suit='spades') 39
Card(rank='Q', suit='clubs') 40
Card(rank='Q', suit='diamonds') 41
Card(rank='Q', suit='hearts') 42
Card(rank='Q', suit='spades') 43
Card(rank='K', suit='clubs') 44
Card(rank='K', suit='diamonds') 45
Card(rank='K', suit='hearts') 46
Card(rank='K', suit='spades') 47
Card(rank='A', suit='clubs') 48
Card(rank='A', suit='diamonds') 49
Card(rank='A', suit='hearts') 50
Card(rank='A', suit='spades') 51
Process finished with exit code 0
通过实现__len__和__getitem__这两个特殊方法,FrenchDeck这个类就像python自有的序列数据类型一样,拥有迭代、切片等python核心语言特性。
练习:创建一个书本类
Book=collections.namedtuple('Book',['name','editor','id'])
# a_book=Book(name='救赎',editor='lily',id='20211101')
# print(a_book)
class MyBook:
names='abandon baby cloud dead'.split()
editors='lili susan yang alice'.split()
ids=[x for x in range(2,6)]
def __init__(self):
self._book=[Book(name,editor,id) for name in self.names for editor in self.editors for id in self.ids] #这种写法对names,editors,ids进行了全排列组合
def __len__(self):
return len(self._book)
def __getitem__(self, item):
return self._book[item]
b=MyBook()
print(len(b))
print(b[2])
执行结果如下:
/Users/zy/PycharmProjects/learnpython/venv/bin/python /Users/zy/PycharmProjects/learnpython/collect.py
64
Book(name='abandon', editor='lili', id=4)
Process finished with exit code 0
标签:__,spades,python,diamonds,rank,第一章,数据模型,suit,Card 来源: https://blog.csdn.net/u011090984/article/details/122010402