《流畅的Python》第一章学习笔记

举报
悦来客栈的老板 发表于 2020/12/28 23:03:11 2020/12/28
【摘要】 1.1 一摞Python风格的纸牌 Python 解释器碰到特殊的句法时,会使用特殊方法去激活一些基本的对象操作,这些特殊方法的名 字以两个下划线开头,以两个下划线结尾(例如__getitem__)。比如 obj[key] 的背后就是 __getitem__ 方法,为了能求得my_collection[key] 的值,解释器实际上会调用 my_co...














  
  1. import collections
  2. Card = collections.namedtuple('Card',['rank','suit']) #需要掌握命名元组的使用
  3. class FrenchDeck:
  4. ranks = [str(n) for n in range(2,11)] + list('JQKA') #13张牌
  5. suits = 'spades diamonds clubs hearts'.split() #4个花色
  6. #ranks和suits都是list类型
  7. def __init__(self):
  8. self._cards = [Card(rank,suit) for suit in self.suits
  9. for rank in self.ranks] #两个for循环使用技巧,需
  10. 要掌握
  11. def __len__(self):
  12. return len(self._cards) #返回列表长度,使对象能直接嗲用len函数
  13. def __getitem__(self,position):
  14. return self._cards[position] #通过列表[下标]返回元素,从而支持getitem功能











  
  1. In [2]: beer_card = Card('7','diamonds')
  2. In [3]: beer_card
  3. Out[3]: Card(rank='7', suit='diamonds')









  
  1. In [4]: deck = FrenchDeck()
  2. In [5]: len(deck)
  3. Out[5]: 52










  
  1. In [6]: deck[0]
  2. Out[6]: Card(rank='2', suit='spades')
  3. In [7]: deck[-1]
  4. Out[7]: Card(rank='A', suit='hearts')









  
  1. In [9]: choice(deck)
  2. Out[9]: Card(rank='5', suit='hearts')
  3. In [10]: choice(deck)
  4. Out[10]: Card(rank='9', suit='spades')
  5. In [11]: choice(deck)
  6. Out[11]: Card(rank='7', suit='hearts')














  
  1. In [12]: deck[:3]
  2. Out[12]:
  3. [Card(rank='2', suit='spades'),
  4. Card(rank='3', suit='spades'),
  5. Card(rank='4', suit='spades')]
  6. In [13]: deck[12:13]
  7. Out[13]: [Card(rank='A', suit='spades')]
  8. In [14]: deck[12::13]
  9. Out[14]:
  10. [Card(rank='A', suit='spades'),
  11. Card(rank='A', suit='diamonds'),
  12. Card(rank='A', suit='clubs'),
  13. Card(rank='A', suit='hearts')]









  
  1. In [16]: for card in deck: #doctest: +ELLIPSIS
  2. ...: print (card)
  3. ...:
  4. Card(rank='2', suit='spades')
  5. Card(rank='3', suit='spades')
  6. Card(rank='4', suit='spades')
  7. Card(rank='5', suit='spades')
  8. Card(rank='6', suit='spades')
  9. Card(rank='7', suit='spades')
  10. Card(rank='8', suit='spades')
  11. Card(rank='9', suit='spades')
  12. .....






  
  1. In [17]: for card in reversed(deck):#doctest:+ELLIPSIS
  2. ...: print (card)
  3. ...:
  4. Card(rank='A', suit='hearts')
  5. Card(rank='K', suit='hearts')
  6. Card(rank='Q', suit='hearts')
  7. Card(rank='J', suit='hearts')
  8. Card(rank='10', suit='hearts')
  9. Card(rank='9', suit='hearts')
  10. Card(rank='8', suit='hearts')
  11. Card(rank='7', suit='hearts')
  12. .......








  
  1. In [19]: Card('Q','hearts') in deck
  2. Out[19]: True
  3. In [20]: Card('7','beass') in deck
  4. Out[20]: False











  
  1. suit_values = dict(spades=3, hearts=2, diamonds=1, clubs=0)
  2. def spades_high(card):
  3. rank_value = FrenchDeck.ranks.index(card.rank) #rank_value,'2' 的值为0,'A'的值为12
  4. return rank_value * len(suit_values) + suit_values[card.suit]







  
  1. >>> for card in sorted(deck, key=spades_high): # doctest: +ELLIPSIS
  2. ... print(card)
  3. Card(rank='2', suit='clubs')
  4. Card(rank='2', suit='diamonds')
  5. Card(rank='2', suit='hearts')
  6. ... (46 cards ommitted)
  7. Card(rank='A', suit='diamonds')
  8. Card(rank='A', suit='hearts')
  9. Card(rank='A', suit='spades')





















































  
  1. from math import hypot
  2. class Vector:
  3. def __init__(self,x=0,y=0):
  4. self.x = x
  5. self.y = y
  6. def __repr__(self):
  7. return 'Vector(%r,%r)' % (self.x,self.y)
  8. def __abs__(self):
  9. return hypot(self.x,self.y)
  10. def __bool__(self):
  11. return bool(abs(self))
  12. def __add__(self,other):
  13. x = self.x + other.x
  14. y = self.y + other.y
  15. return Vector(x,y)
  16. def __mul__(self,scalar):
  17. return Vector(self.x * scalar,self.y * scalar)























  
  1. >>> vect = Vector(3,4)
  2. >>> vect #这里调用了__repr__函数,如果类中没有定义这个函数,这打印类似这
  3. 样的值<Vector object at 0x10e100070>
  4. Vector(3,4)
























































文章来源: blog.csdn.net,作者:悦来客栈的老板,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/qq523176585/article/details/78538957

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。