Card
—Introducing Class Attributes¶FACES
and SUITS
¶FACES
is a list of the card face namesSUITS
is a list of the card suit names# card.py
"""Card class that represents a playing card and its image file name."""
class Card:
FACES = ['Ace', '2', '3', '4', '5', '6',
'7', '8', '9', '10', 'Jack', 'Queen', 'King']
SUITS = ['Hearts', 'Diamonds', 'Clubs', 'Spades']
FACES
and SUITS
(cont.)¶FACES
and SUITS
are constants that are not meant to be modifiedFACES
and SUITS
(cont.)¶FACES
and SUITS
to initialize each Card
we createCard
objectCard.FACES
or Card.SUITS
)__init__
¶__init__
defines a Card
’s _face
and _suit
data attributesdef __init__(self, face, suit):
"""Initialize a Card with a face and suit."""
self._face = face
self._suit = suit
face
, suit
and image_name
¶Card
is created, its face
, suit
and image_name
do not change, so these are read-only propertiesCard
property image_name
’s value is created dynamically by getting the Card
object’s string representation with str(self)
, replacing any spaces with underscores and appending the '.png'
filename extension@property
def face(self):
"""Return the Card's self._face value."""
return self._face
@property
def suit(self):
"""Return the Card's self._suit value."""
return self._suit
@property
def image_name(self):
"""Return the Card's image file name."""
return str(self).replace(' ', '_') + '.png'
Card
provides three special methods that return string representations__repr__
returns a string representation that looks like a constructor expression def __repr__(self):
"""Return string representation for repr()."""
return f"Card(face='{self.face}', suit='{self.suit}')"
__str__
returns a string of the format '
face of
suit'
def __str__(self):
"""Return string representation for str()."""
return f'{self.face} of {self.suit}'
__str__
method of class DeckOfCards
, we use f-strings to format the Card
s in fields of 19 characters eachCard
’s special method __format__
is called when a Card
object is formatted as a stringdef __format__(self, format):
"""Return formatted string representation for str()."""
return f'{str(self):{format}}'
format
parameter’s value as a format specifier, enclose the parameter name in braces to the right of the colon©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.