• Class attributes


    In order to print Card objects in a way that people can easily read, we need a mapping from the integer codes to the corresponding ranks and suits. A natural way to do that is with lists of strings.

    class Card:
        """ represents a standard playing card.
            class attributes: suit_names, rank_names
            instance attributes: suit, rank """
    
        suit_names = ['Clubs','Diamonds','Hearts','Spades']
        rank_names = [None,'Ace','2','3','4','5','6','7',
                      '8','9','10','Jack','Queen','King']
        
        def __init__(self, suit=0, rank=2):
            self.suit = suit
            self.rank = rank
    
        def __str__(self):
            return '%s of %s' % (Card.suit_names[self.suit],
                                 Card.rank_names[self.rank])

    Because suit_names and rank_names are defined outside of any method, they are class attributes; that is, they are associated with the class Card rather than with a particular Card instance. Attributes like suit and rank are more precisely called instance attributes because they are associated with a particular instance. Both kinds of attributes are accessed using dot notation. For example, in __str__, self is a Card object, and self.rank is its rank. Similarly, Card is a class object, and Card.rank_names is a list of strings associated with the class. Every card has its own suit and rank, but there is only one copy of suit_names and rank_names.

    Finally, the expression Card.rank_names[self.rank] means ‘use the attribute rank from the object self as an index into the list rank_names from the class Card, and select the appropriate string’.

    The first element of rank_names is None because there is no card with rank zero. By including None as a place-keeper, we get a mapping with the nice property that index 2 maps to the string ‘2’, and so on.

    Here is a diagram that shows the Card class object and one Card instance:

                           

     from Thinking in Python

  • 相关阅读:
    leetcode 21 Merge Two Sorted Lists
    leetcode 20 Valid Parentheses
    leetcode 19 Remove Nth Node From End of List
    [学习笔记]守护进程深入理解
    [学习笔记]父进程wait和waitpid
    [学习笔记]进程终止的5种方式
    [学习笔记]父子进程共享文件描述符理解
    [学习笔记]Vfork深入理解
    [学习笔记]_exit和exit深入理解
    [学习笔记]孤儿进程僵死进程
  • 原文地址:https://www.cnblogs.com/ryansunyu/p/4004956.html
Copyright © 2020-2023  润新知