San Jose State University Department of Applied Data Science
**DATA 200 Computational Programming for Data Analytics**
Spring 2024 Instructor: Ron Mak
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 10.6.2 Class `Card`—Introducing Class Attributes \n",
"#### Class attributes belong to the class itself, not to individual objects instantiated from the class. They are **shared** by the objects.\n",
"#### Define a class attribute by assigning it a value inside the class definition and not inside any of the class's methods or properties."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Class Attributes `FACES` and `SUITS` \n",
"```python\n",
"class Card:\n",
" \"\"\"\n",
" A playing card.\n",
" Each card object has a face value, a suit, and an image file name.\n",
" \"\"\"\n",
" FACES = ['Ace', '2', '3', '4', '5', '6',\n",
" '7', '8', '9', '10', 'Jack', 'Queen', 'King']\n",
" SUITS = ['Hearts', 'Diamonds', 'Clubs', 'Spades']\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Card Method `__init__` \n",
"```python\n",
" def __init__(self, face, suit):\n",
" \"\"\"\n",
" Construct a Card object.\n",
" \"\"\"\n",
" self._face = face\n",
" self._suit = suit\n",
" self._image_name = str(self).replace(' ', '_') + '.png'\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Read-Only Properties `face`, `suit` and `image_name` \n",
"```python\n",
" @property\n",
" def face(self):\n",
" \"\"\"\n",
" @return the card's face value.\n",
" \"\"\"\n",
" return self._face\n",
"\n",
" @property\n",
" def suit(self):\n",
" \"\"\"\n",
" @return the card's suit.\n",
" \"\"\"\n",
" return self._suit\n",
"\n",
" @property\n",
" def image_name(self):\n",
" \"\"\"\n",
" @return the card's image file name.\n",
" \"\"\"\n",
" return self._image_name\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Methods that Return String Representations of a Card \n",
"```python\n",
" def __repr__(self):\n",
" \"\"\"\n",
" @return the card's official string representation.\n",
" \"\"\"\n",
" return f\"Card(face='{self.face}', suit='{self.suit}', image_name='{self._image_name}')\" \n",
"\n",
" def __str__(self):\n",
" \"\"\"\n",
" @return the cards' string representation for str().\n",
" \"\"\"\n",
" return f'{self.face} of {self.suit}'\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Special method `__format__()`\n",
"#### Python implicitly calls the `__format__()` special method whenever the object is being formatted.\n",
"``` Python\n",
" def __format__(self, format):\n",
" \"\"\"\n",
" @return the card's formatted string representation.\n",
" \"\"\"\n",
" return f'{str(self):{format}}'\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"##########################################################################\n",
"# (C) Copyright 2019 by Deitel & Associates, Inc. and #\n",
"# Pearson Education, Inc. All Rights Reserved. #\n",
"# #\n",
"# DISCLAIMER: The authors and publisher of this book have used their #\n",
"# best efforts in preparing the book. These efforts include the #\n",
"# development, research, and testing of the theories and programs #\n",
"# to determine their effectiveness. The authors and publisher make #\n",
"# no warranty of any kind, expressed or implied, with regard to these #\n",
"# programs or to the documentation contained in these books. The authors #\n",
"# and publisher shall not be liable in any event for incidental or #\n",
"# consequential damages in connection with, or arising out of, the #\n",
"# furnishing, performance, or use of these programs. #\n",
"##########################################################################\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Additional material (C) Copyright 2024 by Ronald Mak"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}