10.13.2 Using the Card Data Class

  • Create a Card`:
In [1]:
from carddataclass import Card
In [2]:
c1 = Card(Card.FACES[0], Card.SUITS[3])
  • Use Card’s autogenerated __repr__ method to display the Card
In [3]:
c1
Out[3]:
Card(face='Ace', suit='Spades')
  • Use custom __str__ method
In [4]:
print(c1)
Ace of Spades
  • Access the data class’s attributes and read-only property
In [5]:
c1.face
Out[5]:
'Ace'
In [6]:
c1.suit
Out[6]:
'Spades'
In [7]:
c1.image_name
Out[7]:
'Ace_of_Spades.png'
  • Demonstrate that Card objects can be compared via the autogenerated == operator and inherited != operator
In [8]:
c2 = Card(Card.FACES[0], Card.SUITS[3])
In [9]:
c2
Out[9]:
Card(face='Ace', suit='Spades')
In [10]:
c3 = Card(Card.FACES[0], Card.SUITS[0])
In [11]:
c3
Out[11]:
Card(face='Ace', suit='Hearts')
In [12]:
c1 == c2
Out[12]:
True
In [13]:
c1 == c3
Out[13]:
False
In [14]:
c1 != c3
Out[14]:
True
  • Card data class is interchangeable with the Card class developed earlier in this chapter
  • To demonstrate this, we created the deck2.py file containing a copy of class DeckOfCards from earlier in the chapter and imported the Card data class into the file
In [15]:
from deck2 import DeckOfCards  # uses Card data class
In [16]:
deck_of_cards = DeckOfCards()
In [17]:
print(deck_of_cards)
Ace of Hearts      2 of Hearts        3 of Hearts        4 of Hearts        
5 of Hearts        6 of Hearts        7 of Hearts        8 of Hearts        
9 of Hearts        10 of Hearts       Jack of Hearts     Queen of Hearts    
King of Hearts     Ace of Diamonds    2 of Diamonds      3 of Diamonds      
4 of Diamonds      5 of Diamonds      6 of Diamonds      7 of Diamonds      
8 of Diamonds      9 of Diamonds      10 of Diamonds     Jack of Diamonds   
Queen of Diamonds  King of Diamonds   Ace of Clubs       2 of Clubs         
3 of Clubs         4 of Clubs         5 of Clubs         6 of Clubs         
7 of Clubs         8 of Clubs         9 of Clubs         10 of Clubs        
Jack of Clubs      Queen of Clubs     King of Clubs      Ace of Spades      
2 of Spades        3 of Spades        4 of Spades        5 of Spades        
6 of Spades        7 of Spades        8 of Spades        9 of Spades        
10 of Spades       Jack of Spades     Queen of Spades    King of Spades     

10.13.3 Data Class Advantages over Named Tuples

  • Objects of different named tuple types could compare as equal if they have the same number of members and the same values for those members
    • Comparing objects of different data classes always returns False, as does comparing a data class object to a tuple object
  • If you have code that unpacks a tuple, adding more members to that tuple breaks the unpacking code
    • Data class objects cannot be unpacked
    • Can add more data attributes to a data class without breaking existing code
  • A data class can be a base class or a subclass in an inheritance hierarchy

10.13.4 Data Class Advantages over Traditional Classes

  • A data class autogenerates __init__, __repr__ and __eq__, saving you time
  • A data class can autogenerate the special methods that overload the <, <=, > and >= comparison operators
  • When you change data attributes defined in a data class, the autogenerated code updates automatically
    • less code to maintain and debug
  • The required variable annotations enable you to take advantage of static code analysis tools
    • Might be able to eliminate additional errors before they can occur at execution time
    • Some static code analysis tools and IDEs can inspect variable annotations and issue warnings if your code uses the wrong type

©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 5 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.

DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.