流畅的python,Fluent Python 第一章笔记

import collections

Card = collections.namedtuple('Card', 'rank suit')

class Frenchdeck:
    ranks = [str(n) for n in range(2, 11)] + list('JQKA')   # 把牌的数字与花色赋值给类属性
    suits = 'spades diamonds clubs hearts'.split()

    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]

    def __repr__(self):
        return f'{self._cards!r}'

deck = Frenchdeck()
print(deck)
print(len(deck))
print(deck[0])
print(deck[1:3])

from random import choice
print(choice(deck))   # 随机算一张牌

print()
# 最后通过通过一个自定义的函数对纸牌进行排序

suit_values = dict(spades=3, hearts=2, diamonds=1,clubs=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):
    print(card)

 

/usr/local/bin/python3.7 /Users/shijianzhong/study/Fluent_Python/第一章/t1.1.py
[Card(rank='2', suit='spades'), Card(rank='2', suit='diamonds'), Card(rank='2', suit='clubs'), Card(rank='2', suit='hearts'), Card(rank='3', suit='spades'), Card(rank='3', suit='diamonds'), Card(rank='3', suit='clubs'), Card(rank='3', suit='hearts'), Card(rank='4', suit='spades'), Card(rank='4', suit='diamonds'), Card(rank='4', suit='clubs'), Card(rank='4', suit='hearts'), Card(rank='5', suit='spades'), Card(rank='5', suit='diamonds'), Card(rank='5', suit='clubs'), Card(rank='5', suit='hearts'), Card(rank='6', suit='spades'), Card(rank='6', suit='diamonds'), Card(rank='6', suit='clubs'), Card(rank='6', suit='hearts'), Card(rank='7', suit='spades'), Card(rank='7', suit='diamonds'), Card(rank='7', suit='clubs'), Card(rank='7', suit='hearts'), Card(rank='8', suit='spades'), Card(rank='8', suit='diamonds'), Card(rank='8', suit='clubs'), Card(rank='8', suit='hearts'), Card(rank='9', suit='spades'), Card(rank='9', suit='diamonds'), Card(rank='9', suit='clubs'), Card(rank='9', suit='hearts'), Card(rank='10', suit='spades'), Card(rank='10', suit='diamonds'), Card(rank='10', suit='clubs'), Card(rank='10', suit='hearts'), Card(rank='J', suit='spades'), Card(rank='J', suit='diamonds'), Card(rank='J', suit='clubs'), Card(rank='J', suit='hearts'), Card(rank='Q', suit='spades'), Card(rank='Q', suit='diamonds'), Card(rank='Q', suit='clubs'), Card(rank='Q', suit='hearts'), Card(rank='K', suit='spades'), Card(rank='K', suit='diamonds'), Card(rank='K', suit='clubs'), Card(rank='K', suit='hearts'), Card(rank='A', suit='spades'), Card(rank='A', suit='diamonds'), Card(rank='A', suit='clubs'), Card(rank='A', suit='hearts')]
52
Card(rank='2', suit='spades')
[Card(rank='2', suit='diamonds'), Card(rank='2', suit='clubs')]
Card(rank='3', suit='hearts')

Card(rank='2', suit='clubs')
Card(rank='2', suit='diamonds')
Card(rank='2', suit='hearts')
Card(rank='2', suit='spades')
Card(rank='3', suit='clubs')
Card(rank='3', suit='diamonds')
Card(rank='3', suit='hearts')
Card(rank='3', suit='spades')
Card(rank='4', suit='clubs')
Card(rank='4', suit='diamonds')
Card(rank='4', suit='hearts')
Card(rank='4', suit='spades')
Card(rank='5', suit='clubs')
Card(rank='5', suit='diamonds')
Card(rank='5', suit='hearts')
Card(rank='5', suit='spades')
Card(rank='6', suit='clubs')
Card(rank='6', suit='diamonds')
Card(rank='6', suit='hearts')
Card(rank='6', suit='spades')
Card(rank='7', suit='clubs')
Card(rank='7', suit='diamonds')
Card(rank='7', suit='hearts')
Card(rank='7', suit='spades')
Card(rank='8', suit='clubs')
Card(rank='8', suit='diamonds')
Card(rank='8', suit='hearts')
Card(rank='8', suit='spades')
Card(rank='9', suit='clubs')
Card(rank='9', suit='diamonds')
Card(rank='9', suit='hearts')
Card(rank='9', suit='spades')
Card(rank='10', suit='clubs')
Card(rank='10', suit='diamonds')
Card(rank='10', suit='hearts')
Card(rank='10', suit='spades')
Card(rank='J', suit='clubs')
Card(rank='J', suit='diamonds')
Card(rank='J', suit='hearts')
Card(rank='J', suit='spades')
Card(rank='Q', suit='clubs')
Card(rank='Q', suit='diamonds')
Card(rank='Q', suit='hearts')
Card(rank='Q', suit='spades')
Card(rank='K', suit='clubs')
Card(rank='K', suit='diamonds')
Card(rank='K', suit='hearts')
Card(rank='K', suit='spades')
Card(rank='A', suit='clubs')
Card(rank='A', suit='diamonds')
Card(rank='A', suit='hearts')
Card(rank='A', suit='spades')

Process finished with exit code 0

 我不知道第一章,第一节就上这么难,反正我是觉的第一次看完全看不懂,现在看算还好,但也要花点脑子。

 

1.2如何使用特殊方法。

len调用的是__len__,对list,str,bytearray会直接返回PyVarObject的ob_size属性。

from math import hypot

class Vector:

    def __init__(self, x=0, y=0):
        self.x = x
        self.y = y

    def __repr__(self):     # 强制repr格式化输出更好
        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, other):
        return Vector(self.x * other, self.y * other)

wrong = Vector('1,','2')
true = Vector(1, 2)
print(wrong)
print(true)
print(true * 3)
print(bool(true))
print(abs(true))

 

/usr/local/bin/python3.7 /Users/shijianzhong/study/Fluent_Python/第一章/t1-2.py
Vector('1,', '2')
Vector(1, 2)
Vector(3, 6)
True
2.23606797749979

Process finished with exit code 0

 1.2.4自定义的布尔值

对一个对象查找bool值,首相调用对象的__bool__方法返回的结果,如没有__bool__就找__len__返回的是否为0,

如果两个方法都没有,返回的就是True

1.3 特殊方法一览

Python中的83个特殊方法的名字,其中43个用于实现算数运算,位运算和比较操作。

跟运算符无关的特殊方法

字符串/字节序列表示转换 __repr__  __str__ __format__  __bytes__

数值转化 __abs__  __bool__  __complex__ __int__ __float__ __hash__ __index__

集合模拟 __len__ __getitem__ __setitem__ __delitem__ __contains__ __missing__(字典可用)

迭代枚举 __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__ <    __le__ <=     __eq__ ==     __ne__ !=  __ge__>=    __gt__>

算数运算符   __add__ +  __sub__ -  __mul__ *  __truediv__ /  __floordiv__ //  __mod__%  __divmod__divmod()   __pow__**或pow()  __round__round()

算数运算符

增量赋值运算符

位运算符

反向位运算符

增量赋值位远算符

后下面这些不写了,在书P11都有。

你可能感兴趣的:(流畅的python,Fluent Python 第一章笔记)