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 ...
随机推荐
- .NET常用方法——邮件发送
邮件发送类文件,可直接使用: 调用方法(实例化.静态调用): 实例化: string exception = ""; SendEmail.SendEmail SE = new Se ...
- TCP非阻塞通信
一.SelectableChannel SelectableChannel支持阻塞和非阻塞模式的channel 非阻塞模式下的SelectableChannel,读写不会阻塞 SelectableCh ...
- (WPF, MVVM) Slider Binding.
对于Button的Command的绑定可以通过实现ICommand接口来进行,但是Slider并没有Command属性. 另外如果要实现MVVM模式的话,需要将一些Method和Slider的Even ...
- android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error(Sqlite code 14): Could not open database,(OS error - 13:Permission denied)
07-24 15:03:14.490 6291-6291/com.tongyan.nanjing.subway E/SQLiteDatabase: Failed to open database '/ ...
- Segment fault及LINUX core dump详解
源自:http://andyniu.iteye.com/blog/1965571 core dump的概念: A core dump is the recorded state of the work ...
- 关于List泛型的强制转换
当我们从数据库中查询出一些数据,有时返回的结果可能是List<Object>类型,而我们清楚的知道它的准确类型是List<User>,可能我们想直接的去进行类型的转换,你可能会 ...
- 帝国CMS常见问题记录
1.第一次安装,为什么找不到"增加信息"的地方?因为你未增加栏目,请先增加栏目,然后再增加信息(终极栏目下方可增加信息). 2.为什么信息管理的"信息栏目"列表不变?缓存问题,解决方法如下两种:1." ...
- 4个理由告诉你Java为何排行第一
Java已经有20年的历史了,甚至更久,而这取决于你所询问的人和你的计算方式.忽略它的年龄不看,Java依然排行第一.它的实用性.性能和向后兼容性都彰显其价值所在.2016年伊始,标志着我们已经走过了 ...
- 修改Jenkins启动衍生进程的生命周期
Jenkins+jmeter 多线程测试java接口时爆错,导致无法生成html报告. 先介绍下场景: 在Jenkins中新建了一个Job,假设你在一些列Build Step之前/之后,启动了一个进程 ...
- C# 导出 Excel 和相关打印设置
源地址:http://blog.csdn.net/wanmingtom/article/details/6125599 Excel.Application myExcel = new Excel.Ap ...