Card objects
There are fifty-two cards in a deck, each of which belongs to one of four suits and one of thirteen ranks. The suits are Spades, Hearts, Diamonds, and Clubs (in descending order in bridge). The ranks are Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, and King. Depending on the game that you are playing, an Ace may be higher than King or lower than 2.
If we want to define a new object to represent a playing card, it is obvious what the attributes should be: rank and suit. It is not as obvious what type the attributes should be. One possibility is to use strings containing words like ‘Spade’ for suits and ‘Queen’ for ranks. One problem with this implementation is that it would not be easy to compare cards to see which had a higher rank or suit.
An alternative is to use integers to encode the ranks and suits. In this context, ‘encode’ means that we are going to define a mapping between numbers and suits, or between numbers and ranks. This kind of encoding is not meant to be a secret.
For example, this table shows the suits and the corresponding integer codes:
Spades -> 3
Hearts -> 2
Diamonds -> 1
Clubs -> 0
This code makes it easy to compare cards; because higher suits map to higher numbers, we can compare suits by comparing their codes.
The class definition for Card looks like:
class Card:
""" represents a standard playing card.""" def __init__(self, suit=0, rank=2):
self.suit = suit
self.rank = rank
As usual, the init method takes an optional parameter for each attribute. The default card is the 2 of Clubs.
from Thinking in Python
Card objects的更多相关文章
- Think Python - Chapter 18 - Inheritance
In this chapter I present classes to represent playing cards, decks of cards, and poker hands.If you ...
- Class diagrams
So far we have seen stack diagrams, which show the state of a program, and object diagrams, which sh ...
- Decks
Now that we have Card objects, the next step is to define a class to represent decks. Since a deck i ...
- Class attributes
In order to print Card objects in a way that people can easily read, we need a mapping from the inte ...
- Django关于filter和get()方法
首先引入一个问题: 问: card = Card.objects.filter(pk=offline_card_id).get() card = Card.objects.get(pk=offline ...
- python测试开发django-37.外键(ForeignKey)查询
前言 前面在admin后台页面通过设置外键,可以选择下拉框的选项,本篇主要讲解关于外键(ForeignKey)的查询 models设计 在上一篇的基础上新增一个BankName表,Card表通过外键关 ...
- python测试开发django-36.一对一(OneToOneField)关系查询
前言 前面一篇在xadmin后台一个页面显示2个关联表(OneToOneField)的字段,使用inlines内联显示.本篇继续学习一对一(OneToOneField)关系的查询. 上一篇list_d ...
- [Django] ModelViewSet from rest_framework and Router
To build rest api easily, we can use ModelViewSet from rest_framework. It provides GET, POST, DELETE ...
- [Django] Building the rest API
Install the rest api framework: pip install djangorestfamework In settings.py: INSTALLED_APPS = [ 'd ...
随机推荐
- hostapd移植与使用
介绍 版本:hostapd-2.5.tar.gz 下载地址:http://w1.fi/releases/hostapd-2.5.tar.gz 依赖:libnl openssl 移植 libnl移植 w ...
- js常见数字处理整理
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...
- 25. Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
- Form_Form Builder国际化多语言开发(案例)
2014-05-06 Created By BaoXinjian
- 转__Android开源项目(三 完结篇)
http://www.csdn.net/article/2013-05-21/2815370-Android-open-source-projects-finale/1 截至目前,在GitHub“最受 ...
- Ecshop(二次开发) - 后台添加左侧菜单导航
1.链接地址:修改 admin\includes\inc_menu.php 文件. $modules['17_syn_data']['view_syn'] = 'synchroni ...
- 使用JS对form的内容验证失败后阻止提交 &&js校验表单后提交表单的三种方法总结
1.form的两个事件 submit,提交表单,如果直接调用该函数,则直接提交表单 onSubmit,提交按钮点击时先触发,然后触发submit事件.如果不加控制的话,默认返回true,因此表单总能提 ...
- iOS 热点、通话时候TabView的Frame调整
- (void)application:(UIApplication *)application didChangeStatusBarFrame:(CGRect)oldStatusBarFrame{ ...
- python 读取文本
将文本转换到NumPy 数组中,做机器学习或其他任何任务,文本处理的技能必不可少.python 实现实现了很精简强大的文本处理功能: 假设 文件 traindata.csv 中有数据 1000行,3列 ...
- Spring切入点表达式常用写法
自从使用AspectJ风格切面配置,使得Spring的切面配置大大简化,但是AspectJ是另外一个开源项目,其规则表达式的语法也稍稍有些怪异. 下面给出一些常见示例的写法: 比如,下面是一个 ...