Smooth python, Fluent Python first chapter notes

Collections Import 

Card collections.namedtuple = ( 'Card' 'Rank SUIT',) 

class Frenchdeck: 
    ranks = [STR (n-) for n-in Range (2,. 11)] + List ( 'JQKA') the card number and # color assigned to the class attribute 
    suits = 'SPADES hearts'.split Diamonds Clubs () 

    DEF the __init __ (Self): # create a formula with a list of cards 
        self._cards = [card (Rank, sUIT) for Rank in self.ranks 
                      for in self.suits SUIT] 

    DEF __len __ (Self): 
        return len (self._cards) 

    DEF the __getitem __ (Self, Item): # define the [] value will be used. 
        self._cards return [Item] 

    DEF __repr __ (Self): 
        return F '{!} R & lt self._cards' 

Deck = Frenchdeck () 
Print (Deck)  
Print (len (Deck))
Print (Deck [0]) 
Print (Deck [. 1:. 3]) 

from Import Choice Random 
Print (Choice (Deck)) # random operator card 

Print () 
# last sort through the card by a custom function 

suit_values = dict (spades = 3, hearts = 2, diamonds = 1, clubs = 0) # define a color value 

DEF spades_high (Card): 
    rank_value = Frenchdeck.ranks.index (card.rank) # card position is taken value 
    return rank_value * len (suit_values) + suit_values [card.suit] # returns the value corresponding to each card 

for card in the sorted (Deck, Key = spades_high): 
    Print (card)

 

/usr/local/bin/python3.7 / Users / shijianzhong / study / Fluent_Python / first chapter /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

 I do not know the first chapter, the first section so hard, anyway, I think the first look totally do not understand, now considered better look, but also to spend the brain.

 

1.2 How to use a special method.

len call is __len__, on the list, str, bytearray returns to the Direct ob_size property PyVarObject of.

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 custom Boolean

Find bool value to an object, the result __bool__ method prime minister call the object's return, if not __bool__ to find __len__ return is 0,

If both methods are not, return is True

1.3 Special Methods List

83 names in Python particular method, wherein arithmetic, bit arithmetic and compare operations 43 for implementation.

Special method with operator-independent

String / byte sequence for converting __repr__ __str__ __format__ __bytes__

Numerical conversion __abs__ __bool__ __complex__ __int__ __float__ __hash__ __index__

A set of analog __len__ __getitem__ __setitem__ __delitem__ __contains__ __missing __ (available dictionary)

Iterative enumeration __iter__ __reversed__ __next__

Callable simulation __call__

Context Management __enter__ __exit__

Examples of creation and destruction __new__ __init__ __del__

Property management __getattr__ __getattribute__ __setattr__ __delattr__ __dir__

Attribute descriptor __get__ __set__ __delete__

Associated with the type of service __prepare__ __instancecheck__ __subclasscheck__

 

Special method associated with operator

Unary operators __neg__ - __pos__ + __abs__ abs ()

Comparison operators __lt__ <__le__ <= __eq__ == __ne__! = __Ge __> = __gt __>

Arithmetic operators __add__ + __sub__ - __mul__ * __truediv__ / __floordiv__ // __mod __% __divmod__divmod () __pow __ ** or pow () __round__round ()

Arithmetic operators

Incremental assignment operator

Bitwise Operators

Reverse bit operators

Augmented assignment bit far operator

After following these do not write, we are in the book P11.

Guess you like

Origin www.cnblogs.com/sidianok/p/12044409.html