《流畅的Python》第一章学习笔记
【摘要】 1.1 一摞Python风格的纸牌
Python 解释器碰到特殊的句法时,会使用特殊方法去激活一些基本的对象操作,这些特殊方法的名
字以两个下划线开头,以两个下划线结尾(例如__getitem__)。比如 obj[key] 的背后就是
__getitem__ 方法,为了能求得my_collection[key] 的值,解释器实际上会调用
my_co...
-
import collections
-
-
Card = collections.namedtuple('Card',['rank','suit']) #需要掌握命名元组的使用
-
-
class FrenchDeck:
-
ranks = [str(n) for n in range(2,11)] + list('JQKA') #13张牌
-
suits = 'spades diamonds clubs hearts'.split() #4个花色
-
#ranks和suits都是list类型
-
-
def __init__(self):
-
self._cards = [Card(rank,suit) for suit in self.suits
-
for rank in self.ranks] #两个for循环使用技巧,需
-
-
要掌握
-
def __len__(self):
-
return len(self._cards) #返回列表长度,使对象能直接嗲用len函数
-
-
def __getitem__(self,position):
-
return self._cards[position] #通过列表[下标]返回元素,从而支持getitem功能
-
In [2]: beer_card = Card('7','diamonds')
-
-
In [3]: beer_card
-
Out[3]: Card(rank='7', suit='diamonds')
-
In [4]: deck = FrenchDeck()
-
-
In [5]: len(deck)
-
Out[5]: 52
-
In [6]: deck[0]
-
Out[6]: Card(rank='2', suit='spades')
-
-
In [7]: deck[-1]
-
Out[7]: Card(rank='A', suit='hearts')
-
In [9]: choice(deck)
-
Out[9]: Card(rank='5', suit='hearts')
-
-
In [10]: choice(deck)
-
Out[10]: Card(rank='9', suit='spades')
-
-
In [11]: choice(deck)
-
Out[11]: Card(rank='7', suit='hearts')
-
In [12]: deck[:3]
-
Out[12]:
-
[Card(rank='2', suit='spades'),
-
Card(rank='3', suit='spades'),
-
Card(rank='4', suit='spades')]
-
-
In [13]: deck[12:13]
-
Out[13]: [Card(rank='A', suit='spades')]
-
-
In [14]: deck[12::13]
-
Out[14]:
-
[Card(rank='A', suit='spades'),
-
Card(rank='A', suit='diamonds'),
-
Card(rank='A', suit='clubs'),
-
Card(rank='A', suit='hearts')]
-
In [16]: for card in deck: #doctest: +ELLIPSIS
-
...: print (card)
-
...:
-
Card(rank='2', suit='spades')
-
Card(rank='3', suit='spades')
-
Card(rank='4', suit='spades')
-
Card(rank='5', suit='spades')
-
Card(rank='6', suit='spades')
-
Card(rank='7', suit='spades')
-
Card(rank='8', suit='spades')
-
Card(rank='9', suit='spades')
-
.....
-
In [17]: for card in reversed(deck):#doctest:+ELLIPSIS
-
...: print (card)
-
...:
-
Card(rank='A', suit='hearts')
-
Card(rank='K', suit='hearts')
-
Card(rank='Q', suit='hearts')
-
Card(rank='J', suit='hearts')
-
Card(rank='10', suit='hearts')
-
Card(rank='9', suit='hearts')
-
Card(rank='8', suit='hearts')
-
Card(rank='7', suit='hearts')
-
.......
-
In [19]: Card('Q','hearts') in deck
-
Out[19]: True
-
-
In [20]: Card('7','beass') in deck
-
Out[20]: False
-
suit_values = dict(spades=3, hearts=2, diamonds=1, clubs=0)
-
def spades_high(card):
-
rank_value = FrenchDeck.ranks.index(card.rank) #rank_value,'2' 的值为0,'A'的值为12
-
return rank_value * len(suit_values) + suit_values[card.suit]
-
>>> for card in sorted(deck, key=spades_high): # doctest: +ELLIPSIS
-
... print(card)
-
Card(rank='2', suit='clubs')
-
Card(rank='2', suit='diamonds')
-
Card(rank='2', suit='hearts')
-
... (46 cards ommitted)
-
Card(rank='A', suit='diamonds')
-
Card(rank='A', suit='hearts')
-
Card(rank='A', suit='spades')
-
from math import hypot
-
-
class Vector:
-
def __init__(self,x=0,y=0):
-
self.x = x
-
self.y = y
-
-
def __repr__(self):
-
return 'Vector(%r,%r)' % (self.x,self.y)
-
-
def __abs__(self):
-
return hypot(self.x,self.y)
-
-
def __bool__(self):
-
return bool(abs(self))
-
-
def __add__(self,other):
-
x = self.x + other.x
-
y = self.y + other.y
-
return Vector(x,y)
-
-
def __mul__(self,scalar):
-
return Vector(self.x * scalar,self.y * scalar)
-
>>> vect = Vector(3,4)
-
>>> vect #这里调用了__repr__函数,如果类中没有定义这个函数,这打印类似这
-
-
样的值<Vector object at 0x10e100070>
-
Vector(3,4)
文章来源: blog.csdn.net,作者:悦来客栈的老板,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/qq523176585/article/details/78538957
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)